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 the interface will contain only the declarations of methods, properties, and events that a class or struct can implement.
An interface in c# is more like a contract,. The class or struct that implements an interface must provide an implementation for all the members specified in the interface definition.
Generally, c# will not support multiple inheritances of classes, but that can achieve by using an interface. Also, a structure in c# cannot be inherited from another structure or class, but that can inherit by using interfaces.
In c#, we can define an interface by using interface
keyword. Following is an example of defining an interface using interface
keyword.
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 an example of implementing an interface in a class.
If you observe the code snippet, we inherited an interface (IUser) in a class (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 the 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, and indexers, but it can’t contain constants, fields, operators, instance constructors, finalizers, or types.
Following is the example of creating and implementing an instance using a class in the c# programming language.
If you observe the 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.
When we execute the above c# program, we will get the result below.
As discussed, c# will not support multiple inheritance of classes, but that can achieve by using the interface.
Following is the example of implementing multiple inheritance using interfaces in the c# programming language.
If you observe an example, we created multiple interfaces and implemented those interfaces using the User class to achieve multiple inheritance.
When you execute the above c# program, you will get the result below.
This is how you can achieve multiple inheritance using the interface in the c# programming language.
The following are the important properties of the interface in the c# programming language.
The following are the differences between abstract class and interface in c# programming language.
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 specified members in the interface definition. |