What Interfaces in C# Are

Interfaces

An interface is a way to declare a set of methods, properties and/or events. It looks like a class, but it does not have any implementation for its members. The class or struct that inherits an interface provides the implementation.


Declaration

To create a class, we use the class keyword as in the following:

  1. class MyClass {  
  2.       

To create an interface, we use an interface keyword as in the following:

  1. interface Iinterface {  
  2.   

Note

It is a good practice to prefix an “I” before the name of the interface to distinguish an interface from the rest of the members.

As we know, an interface can only contain a declaration but no implementation. So, let's look at some of the examples.

1. Method

  1. interface Iinterface {  
  2.     void Sample() {  
  3.         Console.WriteLine("Interface");  
  4.     }  

In the preceding statement, we will get the following error:



So, the interface methods can only have the declaration, in other words the return type, name and a semi-colon.

  1. interface Iinterface {  
  2.     void Sample();  

2. Access Modifier

Another important thing about an interface member is they are public by default and that does not mean you can use a public keyword like this:

  1. interface Iinterface {  
  2.   public  void Sample();  


3. Fields

As we all know, a class can have fields like Name, Id, Gender, and so on but an interface cannot have fields.

  1. interface Iinterface {  
  2.     string name;  




4. Inheritance

  1. interface Iinterface {  
  2.     void Sample();  

The preceding interface that we have created can be implemented in a class or struct. So, let's see how to do that.

There is no different logic for inheriting an interface in a class. To inherit a class from another class we do the following:

  1. class MyClass {  
  2. }  
  3. class MyClassTwo : MyClass {  

To inherit an interface, we use the same logic as in the following:

  1. interface Iinterface {  
  2.     void Sample();  
  3. }  
  4. class MyClass: Iinterface {  
  5.   

5. Implementation

There are two ways to provide the implementation of an interface method and we can use either of those two ways. One using the intelligence and the other is programmatically.

The following describes implicit interface implementation.

1. Programmatically

  1. class MyClass: Iinterface {  
  2.      public void Sample() {  
  3.          Console.WriteLine("Hello");  
  4.      }  
  5.  } 

2. Intellisense

Select the interface, in other words the interface we inherited.

Now use the shortcut key "Ctrl +".



Select, implement the interface "interface" that will give you the following code:

  1. class MyClass: Iinterface {  
  2.      public void Sample() {  
  3.          throw new NotImplementedException();  
  4.      }  
  5.  } 

Now you can remove the default implementation and can provide your own.

Explicit implementation

In implicit implementation, we specify the public access modifier, but in explicit implementation we cannot do that. In explicit implementation, we specify:

Return_type_of_the_method interface_name period(.) name_of_the_method

  1. class MyClass : Iinterface {  
  2.     void Iinterface.Sample() {  
  3.     //the logic  
  4.   

6. Multiple interface inheritance

We can inherit more than one interface in a class as in the following:

  1. interface Iinterface {  
  2.     void Sample();  
  3. }  
  4. interface IInterfaceTwo {  
  5.     void SampleTwo();  
  6. }  
  7. class MyClass : Iinterface,IInterfaceTwo {  
  8.     void Iinterface.Sample() {  
  9.     //  
  10.     }  
  11.     public void SampleTwo() {  
  12.         //  
  13.     }  

7. Instance of an interface

We cannot create an instance of an interface, but we can specify an interface reference variable pointing to a derived class object.

  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         Iinterface i = new Iinterface();  
  4.     }  




Specify an interface reference variable as in the following:

  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         Iinterface i = new MyClass();  
  4.         i.Sample();  
  5.     }  




NOTE

It is a compile time error to provide implementations for any interface members.


Similar Articles