C# Interface
![C# Interface](https://www.wikitechy.com/tutorials/csharp/img/c-sharp-images/c-sharp-interface.png)
What is Interface ?
In c#, the interface is same as a class but the only difference is class can contain both declarations and implementation of methods, properties and events but interface will contain only the declarations of methods, properties, and events that a class or struct can implement.
Why And When To Use Interfaces?
- To achieve security – hide certain details and only show the important details of an object (interface).
- C# does not support “multiple inheritance” (a class can only inherit from one base class). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).
Notes on Interfaces:
- Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an “IAnimal” object in the Program class) OR other-way we can say , an interface cannot be instantiated directly, but it can be instantiated by a class or struct that implements an interface.
- Interface methods do not have a body – the body is provided by the “implement” class
- On implementation of an interface, you must override all of its methods
- Interfaces can contain properties and methods, but not fields/variables
- Interface members are by default
abstract
andpublic
and we are not allowed to include any other access modifiers. - An interface cannot contain a constructor (as it cannot be used to create objects)
- The class or struct can implement multiple interfaces.
- In c#, the interface is like an abstract class and it can contain only declarations of members such as methods, properties, indexers and events.
Difference between Abstract Class and Interface
Abstract Class | Interface |
---|---|
In c#, the abstract classes cannot be instantiated | In c#, an interface cannot be instantiated directly, but it can be instantiated by a class or struct that implements an interface. |
An abstract class can contain both declarations and implementations of methods, properties, etc. | An interface can contain only declarations of methods, properties, indexers and events. |
The members of the abstract class can contain different access modifiers. | By default, all the members of an interface are public and we are not allowed to include any other access modifiers. |
A class can inherit only one abstract class. | A class can inherit multiple interfaces. |
A class that is derived from the abstract class must implement all inherited abstract methods and accessors. | The class or struct that implements an interface must provide an implementation for all the members that are specified in the interface definition. |
How to use Interface
We can define an interface by using interface keyword. Following is the example of defining an interface using interface keyword.
interface IUser { void InsertDetails(); }
If you observe the above code snippet, we defined an interface (IUser) using interface keyword with the InsertDetails method signature. Now, the IUser interface can be implemented by any class or struct by providing a definition for the InsertDetails method.
To implement an interface in a class or structure the syntax will be like class ClassName : Interface Name . Following is the example of implementing an interface in a class.
class User : IUser { void InserDetails() { // Method Implementation } }
If you observe the above code snippet, we inherited an interface (IUser) , in a class named (User) and implemented a defined interface method in a class.
In c#, an interface cannot be instantiated directly , but it can be instantiated by a class or struct that implements an interface. Following is the example of creating an instance for the interface in c# programming language.
IUser u = new User();
In c#, a class can inherit only from one class but we can implement multiple interfaces in a class or struct by using interfaces.
By default, the members of an interface are public and we are not allowed to include any other access modifiers. In c#, an interface can contain methods, properties, events, indexers but it can’t contain constants, fields, operators, instance constructors, finalizers or types.
C# Interface Example
Following is the example of creating and implementing an instance using a class in c# programming language.
using System; namespace Tutlane { interface IUser { void GetDetails(string x); } class User : IUser { public void GetDetails(string a) { Console.WriteLine("Name: {0}", a); } } class User1 : IUser { public void GetDetails(string a) { Console.WriteLine("Location: {0}", a); } } class Program { static void Main(string[] args) { IUser u = new User(); u.GetDetails("Suresh Dasari"); IUser u1 = new User1(); u1.GetDetails("Hyderabad"); Console.WriteLine("\nPress Enter Key to Exit.."); Console.ReadLine(); } } }
If you observe above example, we created an interface IUser and the two classes “User & User1” implemented an interface IUser by providing an implementation for GetDetails() method and we created an instance for interface “IUser” using User & User1 classes.
Output:
C# Multiple Inheritance with Interface
As discussed, c# will not support multiple inheritance of classes but that can be achieved by using the interface.
Following is the example of implementing a multiple inheritance using interfaces in c# programming language.
using System; namespace Tutlane { interface IName { void GetName(string x); } interface ILocation { void GetLocation(string x); } interface IAge { void GetAge(int x); } class User : IName, ILocation, IAge { public void GetName(string a) { Console.WriteLine("Name: {0}", a); } public void GetLocation(string a) { Console.WriteLine("Location: {0}", a); } public void GetAge(int a) { Console.WriteLine("Age: {0}", a); } } class Program { static void Main(string[] args) { User u = new User(); u.GetName("Suresh Dasari"); u.GetLocation("Hyderabad"); u.GetAge(32); Console.WriteLine("\nPress Enter Key to Exit.."); Console.ReadLine(); } } }
If you observe example, we created a multiple interfaces and implementing those interfaces using User class to achieve multiple inheritance.