Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

C# Interface Explained With Examples

1. C# Interface – Introduction

An Interface is a contract, and it is set by declaring a set of functions in it. A class can implement these C# Interfaces. This means, we assure that a class signing the Interface gives the implementation for the contract functions. One can define an Interface as shown below:

Syntax of declaring an C# Interface

Syntax of declaring an C# Interface

2. A Simple Interface

Below is our first simple Interface for Vivo Phone model. Note, the Interface has only functions declared in it and there are no data members.

The above C# Interface defines the signing contract for the old Vivo model phones. When a class implements this Interface, it should serve the functionality of sending a short message and making a voice call. There may be many old models which can sign for the above Interface. But, because of the contract they must implement these contracted functions. Note, each model can have unique implementation. For example, one model can achieve sending a message with a peak text limit of 150 words and another model can have raised limit of 200 words.

3. Implementing a C# Interface

When a class wish to sign a contract, it implements the relevant Interface. The syntax looks same as the inheritance. In the below code, we create a class for an old model Vivo phone which implements our first Interface.

In the above code, we gave the contract functions of making a voice call and sending a text message. Note, we have one function which is not in the contract. This is not a fault as we had fulfilled our contract by implementing voice call and text message.

4. Using IVivo Interface Functions

The client code creates the actual class object, and the reference is stored in the Interface. Through this reference, we can only access the contracted functions. For example, we cannot call the functions other than the short message and voice phone call. The below code shows how client calls the C# Interface functions.

In client code point of view, since the class implemented the IVivo Interface, it knows for definite that class has the functionality for sending a text message and doing a voice call.

Simple Interface - Example 1 - Output

Simple Interface – Example 1 – Output

You can watch creating and using simple c# interface in the youtube video below:

5. Extending an Existing Interface

Like most Object-Oriented Language, C# also supports inheritance. Using this, one can define a derived class from the base class. The same way, a C# Interface can also be extended. In the extended Interface one can add new contracts. In our example, we set up a new Interface called ‘


’ and it extends the Interface ‘


’. Now, we can call ‘


’ as a base Interface.

The new Interface supports video call and sending the Image in ‘PNG’ format. Note, it also has the base Interface functions. Below is the new Interface:

6. Implementing Extended Interface

In the below code we created a class called ‘


’ which implements our new Interface. Since the ‘


’ is a derived Interface of ‘


’, we must write code for both the Interface functions. Below is the code:

7. Using the Extended Interface Functions

When we have the new Extended Interface in hand, we can access all the functionalities offered by it. Note, with this Interface we can also use base ‘


’ Interface functions. In our example below, even though we have only ‘


’ Extended Interface, we can still send a short message through it. This is because, ‘


’ extends ‘


’. Below is the code:

If we create the instance of ‘


’ which implemented the Extended Interface ‘


’, then we can call the Interface functions from both the Interfaces. But, when we use the ‘


’ Interface on this new phone model, we can access the functionalities granted by ‘


’ only. In simple terms, it is not possible to claim ‘


’ Interface functions through ‘


’ even though the class instance has support for it. The below code shows how a base functionality is drawn through base Interface.

Below is the result of executing the extended Interface functions:

Extended Interface - Example 2 Output

Extended Interface – Example 2 Output

The below youtube video shows how to extend an existing interface in C# and use it:

8. Implementing More Than One C# Interface

Like multiple inheritance, a class can implement more than one C# Interface. We will explore this in this section. First, we need to create a new Interface for ‘Htc’ mobile phone. The code is below:

Now we will create a class for merged phone. Here, we will borrow the basic phone functionality from ‘


’ and give advanced functionality from ‘


’. So, the class will sign the contract from both ‘


’ and ‘


’ Interfaces. Below is our new class for the merged phone:

9. Client Code For Merged Phone Class

In the above united phone class, we added the functionality from two C# Interfaces. Client can access these functions with a relevant Interface in hand. For example, if we have HTC Interface we can call HTC functions. But, we cannot call Vivo functions through it. The below picture explains this:

Implementing More Than One Interface

Implementing More Than One Interface

The below code shows how a client uses both the C# Interfaces to call the required functions. Note, we created only one instance of a class but used two Interfaces to get access to a contracted function.

The picture below shows the output:

Multiple Interface - Example 3 Output

Multiple Interface – Example 3 Output

Below youtube video explains how a C# class can implement more than one interface and how we use the class:

10. Casting Between C# Interfaces

A class can implement more than one C# Interface just like our merged phone. In such cases, one can type cast between the Interface types. This will avoid declaring multiple C# Interface references just to hold the actual implementing object.

10.1 Checking The Type Using ‘is’ Keyword

Note, the keyword ‘


’ is in smaller case letters. C# language is case sensitive. With this keyword, one can check a class implements the required Interface or not, and type cast it. Below is the example code:

The code creates an instance of a merged phone. Then, we hold the object in the


reference ‘


’. Now, if we need to use Vivo phone functionality, we can create one more reference of type ‘


’. But, in the above example code (Line 5), we checked that the reference is cast-able as


 and then we did casting (Line 8—9). Note, here the type cast is safe as we already checked it at line 5.

10.2 Casting The Type at Declaration – ‘as’ Keyword

C# can perform type casting during the object declaration. This is done using the ‘


’ keyword. In the below example, we built merged phone instance and pointing it through


reference type. Then, we declared one more reference of type


and performed casting through existing reference of


. The statement ‘

htc2 as IVivo

’ casts the type from





Output of executing the code is below:

Casting Between Interface Safely-Example 4 Output

Casting Between Interface Safely-Example 4 Output

Note, if we use the above technique for unsupported Interface types, the casting returns null. Therefore, we check this at line 8. For Example the below statement will assign null as we know that our merged phone does not support ‘


’ Interface:

Also, the below code is dangerous as the coder should 100% sure ‘


’ have support for ‘


’ Interface. Otherwise, the code will crash. Note, checking null here is not useful as the crash happens in the assignment itself.

Below you video explains how to case between interfaces:

11. Summary

In this article we deep-dived into C# Interfaces. First, we created a simple Interface and then saw how we can extend an existing Interface. We also looked at a class implementing multiple Interfaces. Finally, we looked at how to cast between them safely.

Categories: C#

Tags: , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.