Introduction
An interface in C# is usually used to create loosely coupled and contract-based designs. It can contain signatures (declarations) of the Methods, Properties, Indexers, and Events. The implementation of the methods/properties and so on is done in the class that implements the interface. An interface can inherit one or more interfaces; in other words, it supports multiple inheritance, whereas classes don't.
Implicit interface implementation
This is the most regular or obvious way to implement members of an interface. Here we don't specify the interface name of the members and implement implicitly. The method can be declared at any interface (s) the class implements.
Example
interface ITest
{
void TestMethod();
}
class TestClass : ITest
{
public void TestMethod()
{
Console.WriteLine("Implicit Interface Implementation");
}
}
The call of the method is also not different. Just create an object of the class and invoke it.
class Program
{
static void Main(string[] args)
{
TestClass obj = new TestClass();
obj.TestMethod(); // Way to call implicitly implemented method
}
}
Output
![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXAAAAApCAYAAAAs73K0AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvq
GQAAANrSURBVHhe7d1dktMwDMDxwsnKVbgIl+A0PPUm8MArZ4ARU81otHYiJ4oTtf/fTAdvokiW27jLfsCnH7++/b0BAMr5/PwTAFAMGzgAFMUGDgBFsYEDQFFs4ABQFBs4ABTFBg4ARbGBA0BRH36R5/fPP88RAODKmhv41y/
fnx8BAK6KL6EAQFFs4ABQ1Ets4I/H4/8jS3Y+APO80/27+DXw3iLc7/fn6Bi+7lo9jc+aVyRfZs0j1jl7TUa0+jljHlsd8XxE+LqV1szLfv2N5MuufWUv8Rm4PFGZT1Z2vnfS2/yAWd7p/l3cwP1CzFqYWXWuwvf7Cv1rDxV78XOe1
cOsOngdoR8jXPoriT3nxza+FSdaOZW9psXmEZFcqhUbzefj1FL9iKV+7TlbP9KHisT6mF5dtRTfE8mj1uanonEjlnqx5/zYxrfiRCunstf02FzCx7bq6riVd0s+EcmlejmtzHytXGotVs/LcRu7lPMMaV9CsU3q2C+S8MdaMdki86gic/
1G1mWtjpy3MfqxP97Tisk+dgRbR8eR+eyZXyS/ssd17GO35hO9uK2y87Vcqd+90jZw+8609i4l523M1kXxeVps7rW6kXzCxug1keuy+HqtPpTG2mNiZF0sm89eM8Ln0Dy2bnR+W/vIYuvZcUvG/Eb7teftWG1Zv7U4e05j7TFlz9mYvfmWHNHvmaZ
/E9MuhB0fzdfVRzW+j71G8i2dl3M+l31YcgP4R08vp+fjriZ7fmfly6w78jrIdka/R3iJn0LBdtEbKOvFe9RNGu3jVWT3m51vzYwa74ANHKeQNwT7wHvidbDP9A3cvvPOfBf2dfWBjzfRlW6kkeetUh8ZsvvNzndlRzwfZ9j9m5gaI8d6Y7G0SP6FEqkrzoo
TrdhWXESkrsb05hzpJRIjInV71uJ7NVWrtreljxGRfLbP3lj0cgk/v0hdEYnrzcmO1Wg+a+24FampRvOJXs4tcRrTu9YfP8up38QUMxaiVWNv3as8gT2R+R2xLmtG8kfnd0Yfo/x89swvu9/sfGLt+tH8e+djHdHvWab9e+BXe+cCZuB1jyPxTUwAKIoNHACKY
gMHgKL4PzEBoCg+AweAotjAAaAoNnAAKIoNHACKYgMHgKLYwAGgKDZwACiKDRwAimIDB4Ci2MABoKTb7R+P7NxD6HmjbwAAAABJRU5ErkJggg==)
Explicit interface implementation
This is another way to implement members of an interface. Here we need to specify the; interface name of the members. The following example explains that.
class TestClass : ITest
{
void ITest.TestMethod()
{
Console.WriteLine("Explicit Interface Implementation");
}
}
The constraint with explicit implementation is that an explicitly implemented member cannot be accessed using a class instance, but only through an instance of the interface. Please have a look at the example below.
class Program
{
static void Main(string[] args)
{
ITest obj2 = new TestClass();
obj2.TestMethod();
}
}
Output
![]()
I hope you have liked the article. Please share your comments.