Interfaces
An interface is a way to declare a set of methods, properties and/or events. It looks like a class, but it does not have any implementation for its members. The class or struct that inherits an interface provides the implementation.
Declaration
To create a class, we use the class keyword as in the following:
To create an interface, we use an interface keyword as in the following:
Note
It is a good practice to prefix an “I” before the name of the interface to distinguish an interface from the rest of the members.
As we know, an interface can only contain a declaration but no implementation. So, let's look at some of the examples.
1. Method
- interface Iinterface {
- void Sample() {
- Console.WriteLine("Interface");
- }
- }
In the preceding statement, we will get the following error:
So, the interface methods can only have the declaration, in other words the return type, name and a semi-colon.
- interface Iinterface {
- void Sample();
- }
2. Access Modifier
Another important thing about an interface member is they are public by default and that does not mean you can use a public keyword like this:
- interface Iinterface {
- public void Sample();
- }
3. Fields
As we all know, a class can have fields like Name, Id, Gender, and so on but an interface cannot have fields.
- interface Iinterface {
- string name;
- }
4. Inheritance
- interface Iinterface {
- void Sample();
- }
The preceding interface that we have created can be implemented in a class or struct. So, let's see how to do that.
There is no different logic for inheriting an interface in a class. To inherit a class from another class we do the following:
- class MyClass {
- }
- class MyClassTwo : MyClass {
- }
To inherit an interface, we use the same logic as in the following:
- interface Iinterface {
- void Sample();
- }
- class MyClass: Iinterface {
-
- }
5. Implementation
There are two ways to provide the implementation of an interface method and we can use either of those two ways. One using the intelligence and the other is programmatically.
The following describes implicit interface implementation.
1. Programmatically
- class MyClass: Iinterface {
- public void Sample() {
- Console.WriteLine("Hello");
- }
- }
2. Intellisense
Select the interface, in other words the interface we inherited.
Now use the shortcut key "Ctrl +".
Select, implement the interface "interface" that will give you the following code:
- class MyClass: Iinterface {
- public void Sample() {
- throw new NotImplementedException();
- }
- }
Now you can remove the default implementation and can provide your own.
Explicit implementation
In implicit implementation, we specify the public access modifier, but in explicit implementation we cannot do that. In explicit implementation, we specify:
Return_type_of_the_method interface_name period(.) name_of_the_method
- class MyClass : Iinterface {
- void Iinterface.Sample() {
-
-
- }
6. Multiple interface inheritance
We can inherit more than one interface in a class as in the following:
- interface Iinterface {
- void Sample();
- }
- interface IInterfaceTwo {
- void SampleTwo();
- }
- class MyClass : Iinterface,IInterfaceTwo {
- void Iinterface.Sample() {
-
- }
- public void SampleTwo() {
-
- }
- }
7. Instance of an interface
We cannot create an instance of an interface, but we can specify an interface reference variable pointing to a derived class object.
- class Program {
- static void Main(string[] args) {
- Iinterface i = new Iinterface();
- }
- }
Specify an interface reference variable as in the following:
- class Program {
- static void Main(string[] args) {
- Iinterface i = new MyClass();
- i.Sample();
- }
- }
NOTE
It is a compile time error to provide implementations for any interface members.