This sample code is my favorite way to demonstrate how interface work, I can see students in "20483 Programming in C#" are all understand why we have to implement interface and what is the meaning of contract to the implemented classes.
using System; namespace ConsoleApplication1 { interface IDataAccess { void SelectTop10(); } class OracleDataAccess : IDataAccess { public void SelectTop10(){ Console.WriteLine("SELECT * FROM TABLE1 WHERE ROWNUM <= 10;"); } } class SQLDataAccess : IDataAccess { public void SelectTop10(){ Console.WriteLine("SELECT TOP 10 * FROM TABLE1;"); } } class Program { static void Main(string[] args) { OracleDataAccess oracle = new OracleDataAccess(); oracle.SelectTop10(); IDataAccess sql = new SQLDataAccess(); sql.SelectTop10(); } } } |
Here is the output:-