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
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.
1 2 3 4 5 6 |
//Sample 01: Vivo Old Model Interface public interface IVivo { void SendShortMessage(); void MakeVoiceCall(); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//Sample 02: Class Implementing the Interface public class OldVivoModel: IVivo { public void SendShortMessage() { Console.WriteLine("Sending SMS"); } public void MakeVoiceCall() { Console.WriteLine("Voice call Connected"); } public void OtherInternalFunctions() { Console.WriteLine("Non-Iterface Functionality"); } } |
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.
1 2 3 4 5 6 |
//Sample 03: Using the Interface IVivo Vivo_Old_Phone = new OldVivoModel(); Console.WriteLine("Calling IVivo Functions"); Vivo_Old_Phone.MakeVoiceCall(); Vivo_Old_Phone.SendShortMessage(); Console.WriteLine(); |
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
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 ‘
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:
1 2 3 4 5 6 |
//Sample 04: Extended Interface public interface IVivoNew: IVivo { void MakeVideoCall(); void SendImagePng(); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//Sample 05: New Model offers all base functionality //and offers Video Call and Sending PNG Images class NewVivoModel: IVivoNew { public void SendShortMessage() { Console.WriteLine("Sending SMS"); } public void MakeVoiceCall() { Console.WriteLine("Voice call Connected"); } public void SendImagePng() { Console.WriteLine("Sending Image in PNG Format"); } public void MakeVideoCall() { Console.WriteLine("Turning on the Camera"); Console.WriteLine("Connecting Voice Call" + "with Video"); } } |
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:
1 2 3 4 5 6 7 |
//Sample 06: Using the extended Interface IVivoNew NewVivoPhone = new NewVivoModel(); Console.WriteLine("Calling IVivoNew Functions"); NewVivoPhone.MakeVideoCall(); NewVivoPhone.SendImagePng(); NewVivoPhone.SendShortMessage(); Console.WriteLine(); |
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.
1 2 3 4 5 6 |
//Sample 07: Using new vivo model with base vivo //Interface IVivo base_phone = new NewVivoModel(); Console.WriteLine("Calling IVivo Base Functions"); base_phone.MakeVoiceCall(); Console.WriteLine(); |
Below is the result of executing the extended Interface functions:

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:
1 2 3 4 5 6 |
//Sample 08: HTC Mobile Interface public interface Ihtc { void SendJPGImage(); void SendMp4Video(); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class MergedPhone: IVivo, Ihtc { //Sample 09: This model should provide base //functions of Vivo and functionalities offerred //by htc phone. public void SendShortMessage() { Console.WriteLine("Sending SMS"); } public void MakeVoiceCall() { Console.WriteLine("Voice call Connected"); } public void SendJPGImage() { Console.WriteLine("JPG Image Sent"); } public void SendMp4Video() { Console.WriteLine("Mp4 Streaming started"); } public void NonContractFunction() { Console.WriteLine("An Internal Function"); } } |
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
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Sample 10: Use the Hybrid Phone MergedPhone VivHT = new MergedPhone(); //10.1: Use Vivo Interface and Call Vivo Functionality IVivo VivoOld = VivHT; Console.WriteLine("Calling IVivo functions" + " on Merged phone"); VivoOld.MakeVoiceCall(); Console.WriteLine(); //10.2: Use Htc Interface and call Htc Functionality Ihtc htc = VivHT; Console.WriteLine("Calling IHtc Functions" + " on Merged phone"); htc.SendJPGImage(); htc.SendMp4Video(); Console.WriteLine(); |
The picture below shows the 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
’ 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//Sample 11: Casting between Interfaces MergedPhone merged = new MergedPhone(); Ihtc htc1 = merged; //11.1 Check Vivo functionality available if (htc1 is IVivo) { //11.2 Cast to IVivo and Send Message IVivo vivo_functionality = (IVivo)htc1; Console.WriteLine("Casting Ihtc to IVivo" + " and calling IVivo Functions"); vivo_functionality.SendShortMessage(); Console.WriteLine(); } |
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
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//Sample 12: Cast and Assign Syntax MergedPhone merged2 = new MergedPhone(); Ihtc htc2 = merged2; Console.WriteLine("Casting & Assign Ihtc " + "to IVivo and calling IVivo Functions"); IVivo vivo_anotherExample = htc2 as IVivo; if (vivo_anotherExample != null) { vivo_anotherExample.SendShortMessage(); Console.WriteLine(); } else Console.WriteLine("Interface Not Supported"); |
Output of executing the code is below:

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:
1 2 |
IVivoNew vivo_anotherExample = htc2 as IVivoNew; |
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.
1 |
IVivoNew vivo_anotherExample2 = (IVivoNew) htc2; |
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: C# as Keyword, C# Casting Interfaces, C# Extending Interface, C# Implement Multiple Interface, C# Interfaces, C# is Keyword