Interface Example in C#

By | August 12, 2015

Interface in C#

Interface in C# has been introduced to implement multiple inheritance, which is not possible using the Classes. You can go through my article why .NET does not support Multiple inheritance. Using interfaces we can implement two or more interface contract to a single class which I will show with some interface example. I have said the word contract here as it is mandatory for the derived class to implement all the functions implemented by the interface.

Become an Expert in Programming and Web Development.View all courses!

You can learn about type safety  and type conversion  here.

Interface Example in C#

An interface is a logical group of some desired set of method signatures with the help of which we want to achieve some functionality. Since it is a set of methods that is why events as well as properties(indexers also) can also be contained by an interface. But interface cannot define any constructor methods and neither they can define any instance fields and also it cannot contains any static members. Interface is reduced version of multiple inheritance.  I have mentioned the reduced version as the class implementing the multiple interfaces need to implement all the methods declared in the deriving classes. One of the main feature of the interface is that it allows the instance of the derived types to be used wherever  an interface is being used, which would be useful to understand why interfaces are used. In need to define an interface we use the interface keyword and its set of instance method signatures. And generally it is advised to start the interface with I letter. One of the interface example is as shown below

The above code contains a method signature named DoWork ().
Before going further I want to emphasize on how does CLR treats the interface. For CLR interface is just like defining a class or rather it treats interface definition as type definition. These words can be confirmed by the fact that reflection can be used to query the features of the interface type as shown in the below code.

The interface functions needs to be public in the derived class implementation which is a condition imposed by the C# compiler.
Interface Implementation
1. As I have already defined an interface IEmployee in the above code snippet. Now if I want to implement the interface to my class Engineer as shown below.

Now if I execute the above code I will get an compile time error stating that “’InterfacesInCSharp.Engineer’ does not implement interface member ‘InterfacesInCSharp.IEmployee.DoWork()’” which is obvious as CLR expects each and every function of interface to be defined in the implementing class. The correct implementation of the above code would be as shown below with interface example code

As we can see from the above code the signature of the DoWork() method in the Engineer class is same as the signature of the method in the (IEmployee)Interface.

2.  Just like types, interfaces can be defined at the file scope or can be nested with in a type as shown below or at the file scope

3.  Interface can only “inherit” from other interface. Suppose if we want to extend the implementation of an interface in some part of our project without disturbing the already created interface, in that we can create a new interface and inherit it into new interface as shown below.

Creating a variable for the Interface type

As soon as we are done with the creation and implementation of the interfaces we need to create the variables for them, so that implementing classes can be used to achieve the desired functionality. We can always create the variable of type of the implementing class as shown in the following code for the classes defined in previous snippet.

But it is always suggested to create the variable of interface type containing the reference of the implementing class as shown below

And this concept can be implemented by having a loosely coupled design in the application. Now Suppose I want to my IEmployee to do work, in that case I can always create a function which takes a parameter of type IEmployee and not Engineer type

Value Types Implementing Interfaces
As discussed previously, reference types or classes are not the only one which can implement interfaces. Value types can also implement the zero or more interfaces. The example of which can be shown in the below code snippet.

In the above code I have defined a structure Employee which implements the IEmployee interface which I have defined in the previous code snippet.

Now when we create a variable of the interface containing the reference of the a values type, CLR boxes the values type instance which is a must as interface variable is a reference that must point to an object on the heap so that CLR can examine the object’s type object pointer to determine exact type of the object which is not possible with the case of value types. Now whenever the interface variable is used to call any method of the value type, the object’s boxed type pointer is followed by CLR to find the type object’s method table in order to call the proper method.

Interface Methods are Virtual !!
Yes, you read it right; the interface methods are defined as virtual and sealed by the C# compiler. This thing can be checked by getting into IL code of the interface by using ILDASM.exe . Since the methods are also marked as sealed by complier we cannot override the methods in the derived classes. But if you want to override the method’s implementation in some of the derived class we have to mark the methods as virtual explicitly as shown in the code below for interface example

And a class inheriting from the Engineer class

Explicit Implementation of Interfaces
Suppose I have two interfaces containing the same method signatures as shown below code for interface example

I have a scenario in which I have to implement both of these interfaces in the same class, in that case we will go for explicit interface implementation as shown below for interface example

In explicit implementation both the functions are private and can be accessed by type casting the reference of the class to particular interface as shown below

I hope you have enjoyed reading the article and learned something new in this article about interface example, and share this with your friends. Kindly let know your thoughts about the article.