Please enable Javascript for better experience...
Difference between Implicit and Explicit implementation of Interface
By Rahul Kumar Jha | Nov 13, 2020 | In Articles | Total Views [ 2063 ]
Taged In
(0 Like)

You can implement interface implicitly and explicitly. The difference between both approaches is that if you implement it implicitly then you are bound to call methods via interface only. 


There are two ways to implement interface into your class - Implicitly and Explicitly. And the main difference is that for implicit implementation you become bound to call method via interface only. Let see this by an example.

Code sample

public interface ITest
void Print();
public class Test1 : ITest
public void Print()
Console.WriteLine("Test1.Print() called.");
Console.WriteLine("This is an example of explicit implementation of interface "
+ "and method can be called via interface as well as concrete classs.");
public class Test2 : ITest
void ITest.Print()
Console.WriteLine("Test2.Print() called.");
Console.WriteLine("This is an example of implicit implementation of interface "
+ "and method can only be called via interface.");

You can notice in above example that in class Test2, we have implemented interface method Print() with its name like void ITest.Print() and also we have not added amy access modifier to this. This is an example of iimplicit implementation. On the other hand if you see implementation of same method in class Test1, we have done this as public void Print(). This is explicit implementation. Now let see what is the difference between two.

class Program
static void Main(string[] args)
ITest testInterface = new Test1();
Test1 test1Class = new Test1();
testInterface = new Test2();

Till above point it will work fine and here is the output.


But if try to write below code, it will throw error stating that Test2 does not contains definition of Print() and no accessible extension method Print() accepting first argument of type Test2 could be found

Test2 test2Class = new Test2();

Hope this helps.

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer

Public profile: user/profile/99900001

Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments

Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID