An Interface is a contract that is implemented. It has just the method definition inside. Implementation of these methods is done by the class implementing the interface. We can use properties, indexers in the inteface too, but we can just define them. Implementation will be done by the implementing class of the interface.

Interface is by default public in nature. The class inheriting the interface has to compulsorily implement all the interface methods. Let us check out the syntax for interface.

Syntax

  1. interface ITest  
  2.    {  
  3.        void Read();  
  4.    }  
Interface is a keyword following with the interface name. It contains the method definition.Let us check one program to see how we use interface.
  1. interface ICalculate  
  2. {  
  3.     int Add(int a,int b);  
  4.     int Sub(int a, int b);  
  5.     int Mul(int a, int b);  
  6. }  
  7.   
  8. class Test : ICalculate  
  9. {  
  10.     public int Add(int a,int b)  
  11.     {  
  12.         return a + b;  
  13.     }  
  14.   
  15.     public int Sub(int a, int b)  
  16.     {  
  17.         return a - b;  
  18.     }  
  19.   
  20.   
  21.     public int Mul(int a, int b)  
  22.     {  
  23.         return a * b;  
  24.     }  
  25.   
  26. }  
  27.   
  28. class Program  
  29. {     
  30.     static void Main(string[] args)  
  31.     {  
  32.         Test obj = new Test();  
  33.         ICalculate iadd = (ICalculate)obj;  
  34.   
  35.         int add= iadd.Add(10, 20);  
  36.   
  37.         int sub = iadd.Sub(30,10);  
  38.   
  39.         int mul = iadd.Mul(20,30);  
  40.   
  41.         Console.WriteLine(" Addition : {0} Subtraction : {1} Multiplication : {2} ", add, sub, mul);  
  42.   
  43.         Console.ReadLine();  
  44.   
  45.     }  
  46. }  
In the preceding example we have an ICalculate interface that has three method declarations. A class is created to implement the interface. So it has to compulsorily implement all the interface methods.

Output




What if there are two interfaces having the same method names? And if both of them are implemented by a class then how to define them explicitly?Consider the following code.
  1. interface A  
  2.    {  
  3.        void Display();  
  4.    }  
  5.   
  6.    interface B  
  7.    {  
  8.        void Display();  
  9.    }  
Both of the interfaces have same method names. Now a class Test implements these interface. So in this scenario we have something called define interface explicitly. Refer the following snapshot.



When we hover above the interface while implementing them go to Implement Interface-> Implement Interface explicitly. And it will automatically implement the methods of both of the interfaces followed by the interface name. Check the following code. We can write our logic inside the methods now.
  1. void A.Display()  
  2.          {  
  3.             Console.WriteLine("I am being called from Interface A");  
  4.          }  
  5.   
  6.           
  7.         void B.Display()  
  8.         {  
  9.             Console.WriteLine("I am being called from Interface B");  
  10.         }  
Complete Source Code
  1. interface A  
  2.     {  
  3.         void Display();  
  4.     }  
  5.   
  6.     interface B  
  7.     {  
  8.         void Display();  
  9.     }  
  10.   
  11.     class Test:A,B  
  12.     {  
  13.   
  14.          void A.Display()  
  15.          {  
  16.             Console.WriteLine("I am being called from Interface A");  
  17.          }  
  18.   
  19.           
  20.         void B.Display()  
  21.         {  
  22.             Console.WriteLine("I am being called from Interface B");  
  23.         }  
  24.   
  25.           
  26.     }  
  27.   
  28.     class Program  
  29.     {  
  30.         static void Main(string[] args)  
  31.         {  
  32.             Test obj1 = new Test();  
  33.             A a = (A)obj1;  
  34.             a.Display();  
  35.   
  36.             Test obj2 = new Test();  
  37.             B b = (B)obj2;  
  38.             b.Display();  
  39.   
  40.             Console.ReadLine();  
  41.   
  42.   
  43.              
  44.         }  
  45.     }  
We can create an object of the interface in a different way as well. Check the following code.
  1. A a = new Test();  
  2. a.Display();  
Here we have created an object of Interface A and cast it to the class type. It will work perfectly fine as well.

Output

Next Recommended Readings