17 - Interface in C#

17.1 Purpose of Interfaces

An interface in C# has its structure similar to a class but it does not contain any implementation. Interface defines properties, methods, and events that are known as members of it. Classes and structures also implement these interface members. They are used as a replacement to the use of multiple inheritance, as it is not supported in C#.

An interface is used when you want a standard structure of methods to be followed by the classes, where the classes implement the functionality. Interface defines what a class can do, but it does not define how the class does it.

The different purpose of using interfaces is as mentioned below:

1) It is used for the reuse of the software

2) It creates loosely coupled software

3) It allows different objects to interact easily with each other

4) It separates the definition of objects from their implementation

5) It provides user to achieve runtime polymorphism

6) Interface inheritance provides the concept of name hiding. It is an ability to hide an inherited member from the code outside the derived class

17.2 Define an Interface

Working with interfaces includes declaration and implementation of interfaces by the classes. User can declare only methods, functions and properties in interfaces. You cannot declare a variable in interface.

User can declare an interface by using the interface keyword. The declaration of an interface is similar to a class declaration. The statements in an interface are public. The following code snippet shows the declaration of an interface.

The interface named IMyInterface is defined in the above snippet. According to the naming conventions, the prefix of the interface name should be with capital letter ‘I’. The interface declares a single method as MethodToImplement(). The method does not contain any implementation but it ends with a semicolon. The interface specifies only the signature of the methods that a inheriting class or a struct must implement.

A code sample demonstrating the implementation of an interface is as shown below:

The output for the code is as shown below

:

In the above code, the interface named IGame is defined. It consists of variables as noofplayers, sport type, width and breadth of the ground. The variables are initialized in the constructor of the class. The method for displayed the output is defined.

17.3 Use an Interface

There are different uses of an interface in C# are as mentioned below:

1) Interface helps user to define the behavioral model, defining what an object can perform in an application

2) It reduces the coupling and allows the user to easily change the implementations without affecting the code

3) If every class needs different implementations, better is the use of the concept of interface

4) The IDisposable interface guarantees a user that it has an Dispose() method

5) Interfaces can implement another interface in same application

17.4 Implement the Interface Inheritance

A class inherits all the interfaces defined in the same application. The interface can be inherited from another interface defined by the user. These interfaces can be then inherited by the class defining the methods for the interface definitions.

The general syntax of implementing the inheritance interface is as shown below:

A class or a structure that implements interfaces also implements the base interfaces of its inherited interfaces. In the above code, the IResult interface inherits the member function, ParentInterface(), from the base interface IParent. If the class implements the IResult interface, it implicitly implements the IParent interface.