Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# Interfaces

C# Interfaces

An interface defines the syntax contract that all classes inheriting the interface should follow. An interface defines the syntax contract "what it is" part, while derived classes define the syntax contract "how to do it" part.

An interface defines properties, methods, and events, which are all members of the interface. An interface contains only declarations of members. The definition of members is the responsibility of the derived class. An interface provides a standard structure that derived classes should follow.

Interfaces make classes or structs that implement them consistent in form.

Abstract classes are somewhat similar to interfaces, but they are mostly used when only a few methods are declared by the base class and implemented by derived classes.

An interface itself does not implement any functionality; it merely establishes a contract with objects that declare they implement the interface, specifying which behaviors must be implemented.

An abstract class cannot be instantiated directly, but it allows the derivation of concrete classes with actual functionality.


Defining an Interface: MyInterface.cs

An interface is declared using the interface keyword, which is similar to a class declaration. Interface declarations are public by default. The following is an example of an interface declaration:

interface

IMyInterface

{

   

void

MethodToImplement

(

)

;

}

The above code defines the interface IMyInterface. Interfaces are usually named starting with the letter I. This interface has only one method, MethodToImplement(), with no parameters and no return value; of course, we can set parameters and return values as needed.

It is worth noting that this method has no concrete implementation.

Next, let's implement the above interface: InterfaceImplementer.cs

Example

using

System

;

interface

IMyInterface

{

       

// Interface members

   

void

MethodToImplement

(

)

;

}

class

InterfaceImplementer

:

IMyInterface

{

   

static

void

Main

(

)

   

{

        InterfaceImplementer iImp

=

new

InterfaceImplementer

(

)

;

        iImp

.

MethodToImplement

(

)

;

   

}

   

public

void

MethodToImplement

(

)

   

{

        Console

.

WriteLine

(

"MethodToImplement() called."

)

;

   

}

}

The InterfaceImplementer class implements the IMyInterface interface; interface implementation is similar in syntax to class inheritance:

class InterfaceImplementer : IMyInterface

After inheriting an interface, we need to implement the interface's method MethodToImplement() ; the method name must be consistent with the one defined in the interface.


Interface Inheritance: InterfaceInheritance.cs

The following example defines two interfaces, IMyInterface and IParentInterface.

If an interface inherits other interfaces, then the implementing class or struct needs to implement all the members of the interfaces.

In the following example, IMyInterface inherits from the IParentInterface interface, so the implementing class must implement the MethodToImplement() and ParentInterfaceMethod() methods:

Example

using

System

;

interface

IParentInterface

{

   

void

ParentInterfaceMethod

(

)

;

}

interface

IMyInterface

:

IParentInterface

{

   

void

MethodToImplement

(

)

;

}

class

InterfaceImplementer

:

IMyInterface

{

   

static

void

Main

(

)

   

{

        InterfaceImplementer iImp

=

new

InterfaceImplementer

(

)

;

        iImp

.

MethodToImplement

(

)

;

        iImp

.

ParentInterfaceMethod

(

)

;

   

}

   

public

void

MethodToImplement

(

)

   

{

        Console

.

WriteLine

(

"MethodToImplement() called."

)

;

   

}

   

public

void

ParentInterfaceMethod

(

)

   

{

        Console

.

WriteLine

(

"ParentInterfaceMethod() called."

)

;

   

}

}

The example output is:



MethodToImplement() called.

ParentInterfaceMethod() called.