Implicit and Explicit Interface Examples

An interface can contain signatures (declarations) of the Methods, Properties, Indexers and Events.

The implementation of the methods is done in the class that implements the interface.

A Delegate is a type that can't be declared in an interface. You can either use an event (if appropriate) or declare a delegate outside the interface but in the same namespace.

Interfaces in C# provides a way to achieve runtime polymorphism. Using interfaces, we can invoke functions from various classes through the same Interface reference, whereas using virtual functions we can invoke functions from various classes in the same inheritance hierarchy through the same reference.

An interface can inherit from one or more base interfaces.

A class that implements an interface can explicitly implement members of that interface.

An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.

Purposes of Interfaces

Create loosely coupled software.

Support design by contract (an implementer must provide the entire interface).

Allow for pluggable software.

Allow objects to interact easily.

Hide implementation details of classes from each other.

Facilitate reuse of software.

Example 1

interfaceIinterface_1

{

}

classClass1 : Iinterface_1

{

publicvoid Display()

{

Console.WriteLine("Class1 Display Method.");

}

}

classProgram

{

staticvoid Main(string[] args)

{

Class1 objClass1 = newClass1();

objClass1.Display();

}

}

Output: Class1 Display Method

Example 2

The following is an example of Implicit and Explicit interface implementation:

interfaceIinterface_1

{

void interface1_method();

}

classClass1 : Iinterface_1

{

publicvoid Display()

{

Console.WriteLine("Class1 Display Method.");

}

//Implicit interface implementation

publicvoid interface1_method()

{

Console.WriteLine("Iinterface_1 Method.");

}

//Explicit interface implementation

voidIinterface_1.interface1_method()

{

Console.WriteLine("Iinterface_1 Method.");

}

}

classProgram

{

staticvoid Main(string[] args)

{

Class1 objClass1 = newClass1();

objClass1.Display();

}

}

Output: Class1 Display Method.

Example 3

The following is an example of how to call an Implicit interface method:

The following is an example of how to call "Two interface methods" separately in a class.

interfaceIinterface_1

{

void interface1_method();

}

interfaceIinterface_2

{

void interface2_method();

}

classClass1 : Iinterface_1, Iinterface_2

{

publicvoid Display()

{

Console.WriteLine("Class1 Display Method.");

}

publicvoid interface1_method()

{

Console.WriteLine("Iinterface_1 Method.");

}

publicvoid interface2_method()

{

Console.WriteLine("Iinterface_2 Method.");

}

}

classProgram

{

staticvoid Main(string[] args)

{

Iinterface_1 obj_1 = newClass1();

obj_1.interface1_method();

Iinterface_2 obj_2 = newClass1();

obj_2.interface2_method();

Console.ReadLine();

}

}

Output: Class1 Display Method.

Iinterface_1 Method.Iinterface_2 Method.

Note:

1. If you try to call "Interface2_method()" with "obj_1" object, then it will give you a compile error, as in://Error//obj_1.interface2_method(); //'Iinterface_1' does not contain a definition for 'interface2_method'