C# Interface Explained With Examples

[toc]

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

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 ‘ IVivoNew’ and it extends the Interface ‘ IVivo’. Now, we can call ‘ IVivo’ 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 ‘ NewVivoModel’ which implements our new Interface. Since the ‘ IVivoNew’ is a derived Interface of ‘ IVivo’, 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 ‘ IVivo’ Interface functions. In our example below, even though we have only ‘ IVivoNew’ Extended Interface, we can still send a short message through it. This is because, ‘ IVivoNew’ extends ‘ IVivo’. Below is the code:

If we create the instance of ‘ NewVivoModel’ which implemented the Extended Interface ‘ IVivoNew’, then we can call the Interface functions from both the Interfaces. But, when we use the ‘ IVivo’ Interface on this new phone model, we can access the functionalities granted by ‘ IVivo’ only. In simple terms, it is not possible to claim ‘ IVivoNew’ Interface functions through ‘ IVivo’ 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

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 ‘ IVivo’ and give advanced functionality from ‘ IHtc’. So, the class will sign the contract from both ‘ IVivo’ and ‘ IHtc’ 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

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’ 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 Ihtc reference ‘ htc1’. Now, if we need to use Vivo phone functionality, we can create one more reference of type ‘ IVivo’. But, in the above example code (Line 5), we checked that the reference is cast-able as IVivo 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 ‘ as’ keyword. In the below example, we built merged phone instance and pointing it through Ihtc reference type. Then, we declared one more reference of type IVivo and performed casting through existing reference of Ihtc. The statement ‘ htc2 as IVivo’ casts the type from Ihtc to IVivo.

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 ‘ IVivoNew’ Interface:

Also, the below code is dangerous as the coder should 100% sure ‘ htc2’ have support for ‘ IVivoNew’ Interface. Otherwise, the code will crash. Note, checking null here is not useful as the crash happens in the assignment itself.

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.

%d bloggers like this: