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
- interface ITest
- {
- void Read();
- }
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.
- interface ICalculate
- {
- int Add(int a,int b);
- int Sub(int a, int b);
- int Mul(int a, int b);
- }
-
- class Test : ICalculate
- {
- public int Add(int a,int b)
- {
- return a + b;
- }
-
- public int Sub(int a, int b)
- {
- return a - b;
- }
-
-
- public int Mul(int a, int b)
- {
- return a * b;
- }
-
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- Test obj = new Test();
- ICalculate iadd = (ICalculate)obj;
-
- int add= iadd.Add(10, 20);
-
- int sub = iadd.Sub(30,10);
-
- int mul = iadd.Mul(20,30);
-
- Console.WriteLine(" Addition : {0} Subtraction : {1} Multiplication : {2} ", add, sub, mul);
-
- Console.ReadLine();
-
- }
- }
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.
OutputWhat 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.
- interface A
- {
- void Display();
- }
-
- interface B
- {
- void Display();
- }
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.
- void A.Display()
- {
- Console.WriteLine("I am being called from Interface A");
- }
-
-
- void B.Display()
- {
- Console.WriteLine("I am being called from Interface B");
- }
Complete Source Code
- interface A
- {
- void Display();
- }
-
- interface B
- {
- void Display();
- }
-
- class Test:A,B
- {
-
- void A.Display()
- {
- Console.WriteLine("I am being called from Interface A");
- }
-
-
- void B.Display()
- {
- Console.WriteLine("I am being called from Interface B");
- }
-
-
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- Test obj1 = new Test();
- A a = (A)obj1;
- a.Display();
-
- Test obj2 = new Test();
- B b = (B)obj2;
- b.Display();
-
- Console.ReadLine();
-
-
-
- }
- }
We can create an object of the interface in a different way as well. Check the following code.
- A a = new Test();
- 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