Interfaces in C# are a powerful tool for achieving abstraction and allowing objects to interact with each other. They define a set of methods, properties, and events that a class must implement, but do not provide an implementation for them. This allows for greater flexibility and reusability of code.
Abstraction in software development is a process of hiding the details of an implementation from other parts of the system. This is done to reduce complexity and allow developers to work on the same codebase without needing to understand all the details. Abstraction also allows developers to focus on the functionality instead of the implementation details.
What is a C# Interface?
An interface looks like a class but has no implementation. The only thing it contains is declarations of events, indexers, methods, and/or properties. The reason interfaces only provide declarations is that they are inherited by structs and classes, which must provide an implementation for each interface member declared.
So, what are interfaces good for if they don't implement functionality? They're great for putting together plug-and-play-like architectures where components can be interchanged at will. Since all interchangeable components implement the same interface, they can be used without any extra programming. The interface forces each component to expose specific public members that will be used in a certain way.
Because interfaces must be implemented by derived structs and classes, they define a contract.
interface IMyInterface {
void MethodToImplement(); //Abstract Method signature.
}
class InterfaceImplementer: IMyInterface {
static void Main() {
InterfaceImplementer obj = new InterfaceImplementer();
obj.MethodToImplement();
}
public void MethodToImplement() {
//Abstract Method Implementation
}
}
Here, we advised to use "I" as the prefix for the interface to understand that the interface is an interface.
Why Use an Interface in C#?
I hope everyone is quite familiar with OOP concepts in C++. (Class and Object, Inheritance and its type, and so on). Inheritance allows the creation of classes that are derived from other classes so that they automatically include some of its "parent's" members, plus their own. The following are types of inheritances.
Interfaces are useful for a number of reasons. They allow for greater abstraction, making it easier to write flexible and reusable code. They also make it possible for objects of different types to interact with each other, as long as they implement the same interface. This is known as polymorphism.
Some popular reasons to use Interfaces in software development include:
-
To reduce code complexity and improve maintainability: Interfaces act as a contract between different components of a system, allowing developers to break down their code into modular components and easily maintain and update them.
-
To allow for loose coupling: Interfaces allow developers to create loosely coupled systems that are more flexible and extensible.
-
To improve extensibility: Interfaces provide a way for developers to easily extend their applications with new features and capabilities.
-
To enable polymorphism: Interfaces allow developers to write code that can be used with different types of objects, enabling polymorphism and code reuse.
-
To promote code reusability: Interfaces provide a way for developers to write code that can be reused across multiple projects.
How to Create a C# Interface
Creating an interface in C# is easy. Simply use the interface keyword followed by the name of the interface and a set of method and property signatures. For example:
interface IMyInterface
{
void MyMethod();
int MyProperty { get; set; }
}
Implementing an Interface
A class can implement an interface by using the : operator followed by the interface name. The class must then provide an implementation for all of the methods and properties defined in the interface. For example:
class MyClass : IMyInterface
{
public void MyMethod()
{
// Implementation details here
}
public int MyProperty { get; set; }
}
Interface Inheritance
Interfaces can also inherit from other interfaces, using the : operator. For example:
interface IMyBaseInterface
{
void MyBaseMethod();
}
interface IMyDerivedInterface : IMyBaseInterface
{
void MyDerivedMethod();
}
A class can implement multiple interfaces, separated by commas. For example:
class MyClass : IMyInterface1, IMyInterface2
{
// Implementation details here
}
Conclusion
Interfaces are a powerful tool in C#, allowing for abstraction and the implementation of polymorphism. They are easy to create and implement, and can also be inherited from other interfaces. It helps to create a contract for the class to follow, and also helps with better code organization and maintenance.

Bounty PWPosted Mar 1, 2021, 8:04 PM
Idk bad example, you could have done that without interface
Mohd Naved AlamPosted May 18, 2020, 4:00 PM
Why are we creating an object of the interface IFive in Program.cs. It's not a complete or concrete class. Shouldn't we create an object of class ODDEVEN ?
Midhushan MohanPosted Mar 2, 2020, 10:40 PM
IT'S Very Helpful Thanks
Gabriel Espirito SantoPosted May 23, 2019, 8:48 AM
Great example of multiple inheritance !!
Mangesh GPosted May 23, 2019, 1:15 AM
Multiple inheritance is the. Only reason for interfaces??? Lets not restrict interfaces to it they are much more valuable thaN that
Madan ShekarPosted Jan 24, 2019, 5:29 AM
Amazing explanation... i understand very well by reading this
DeepanPosted Dec 24, 2018, 11:43 PM
Clear idea with proper explanation.. thanks..
Naveen BishtPosted Dec 19, 2018, 4:23 AM
Why c# not support multiple inheritance ? can you give some summery about this
Mohanraj KaliappanPosted Dec 19, 2018, 12:15 AM
Nice Article
Kokilan YogalingamPosted Dec 18, 2018, 10:43 PM
Nice Article
Jipson ThomasPosted Oct 30, 2018, 6:57 AM
Sweet and short. Really helpful.
Kaushal PareekPosted Oct 5, 2018, 4:13 AM
Nice Explanation. Thanks
Dharmendra Kumar PanditPosted Aug 13, 2018, 11:15 AM
Nice Article ......
Sanjay ChauhanPosted May 8, 2018, 1:48 AM
Nice Explanation. Thanks
Basma WemalakPosted Jul 3, 2017, 8:04 AM
Thanks about the article,i have a question,Can i implicitly implement the members of both interfaces and then access their members via an object of the class?
Peter HassanPosted Jul 3, 2017, 6:31 AM
Great Article - Thank you
Balakrishnan GPosted May 17, 2017, 3:29 PM
Its so nice to understand .Thanks a lot.
Ganesh PawarPosted Apr 2, 2017, 2:32 AM
Nice Bala, Well Explained!
Matt YalePosted Mar 8, 2017, 10:08 PM
Hi Bala, minor thing but you typo'd three as there in your code and example Console.WriteLine("This is THERE");
srinivasarao kotaPosted Oct 19, 2016, 5:59 AM
Nice
SubashPosted Sep 15, 2016, 7:43 AM
Nice share
Ramesh PalaniappanPosted Aug 29, 2016, 11:24 AM
Nice
Arjun DhilodPosted Jul 29, 2016, 9:02 AM
Great articular
Haribansh Kumar AgrawalPosted May 13, 2016, 6:46 AM
Well Explained
Mohammed IbrahimPosted Jan 20, 2016, 10:04 AM
nice
Jalaja RajuPosted Dec 16, 2015, 8:19 PM
Hi, In the authors example, IEVEN instance can access only even numbers and IFIVE (IODD exactly) can access only the odd numbers. But in your example, the instance of the class can access all the methods. So later point of time we can use IFIVE for finding a prime number etc..
Vaibhav AgarwalPosted Nov 3, 2015, 10:58 AM
using System; namespace vaibhav { class ODDEVEN { public void ONE() { Console.WriteLine("This is ONE"); } public void TWO() { Console.WriteLine("This is TWO"); } public void THREE() { Console.WriteLine("This is THERE"); } public void FOUR() { Console.WriteLine("This is FOUR"); } public void FIVE() { Console.WriteLine("This is FIVE"); } } public class SametaskWithoutinterface { public static void Main() { Console.WriteLine("This is ODD"); ODDEVEN obj1 = new ODDEVEN(); obj1.ONE(); obj1.THREE(); obj1.FIVE(); Console.WriteLine("\n\nThis is EVEN"); ODDEVEN obj2 = new ODDEVEN(); obj2.TWO(); obj2.FOUR(); Console.ReadLine(); } } }
Vaibhav AgarwalPosted Nov 3, 2015, 10:52 AM
I never understand one thing that Why did we use that ... Same output everyone can do using without interface , Suppose in your above example i dont define the interface and no inherit the interface and everything same then what is the problem ?
Hasan KhanPosted Oct 29, 2015, 3:59 AM
Nice Dear
Kamal ManiPosted Oct 6, 2015, 9:38 AM
Nice
Ram PradeepPosted Sep 16, 2015, 8:38 AM
all is ok c# dosent support multiple inheritance because of that it came into existence.mydoubt is why the it dont contain implementation and why it will not allow to create instance.
Mohammad KhalidPosted Jun 4, 2015, 7:56 AM
Good Job Done...
pramod kumar mishaPosted Apr 1, 2015, 6:23 AM
good example
Md. Raskinur RashidPosted Jan 8, 2015, 11:25 AM
visual presentation is very much understandable. Thanx!
sumit singhPosted Jun 26, 2014, 7:05 AM
nyc