Click here to Skip to main content
15,886,873 members
Articles / Programming Languages / C#
Tip/Trick

What is the Purpose of "explicit" Implementation of Interface?

Rate me:
Please Sign up or sign in to vote.
4.67/5 (5 votes)
1 Jun 2014CPOL1 min read 15.6K   4   5
Purpose of "Explicit" Implementation of Interface in C#

Introduction

There are 2 types of interface implementations available in C#. One is Implicit and another one is Explicit. This tip explains the purpose of the "Explicit" implementation of Interfaces.

Background

Interfaces are the one of the best language features in C#, which helps to achieve the run time polymorphic feature. Every programmer has experience with interfaces, with implicit implementation, which is normal. This article talks about another way of implementation and its purposes.

Using the Code

There are 2 purposes of explicit implementation of Interface:
First, it is used to avoid name collision between interface methods. That is if you are going to create a class library, there may be a chance to use the same name in several places. At that time "explicit" implementation comes as a rescue.
Secondly, You cannot access that implemented method through the object of the class directly. Instead you typecast it as Interface reference then you can access it. This is because, the C# compiler, unable to determine which one the user want to call.

In another way, you need to add "public" access specifier in the interface's implemented method in a class. But if you "explicitly" implemented the interface, you can change this one. That means, it will be look alike private method. But with one exception, you can access that explicit implemented method through the reference of the interface. Please take a look at the below code for a detailed explanation:

C#
interface ISample
{
    void method1();
    void method2();
}

interface IAnotherSample
{
    void method1();
}

class A : ISample, IAnotherSample
{
    // Explicit implementation avoid name collision
    void ISample.method1()
    {
    }
    void IAnotherSample.method1()
    {
    }
    // Implicit implementation
    public void method2()
    {
    }
}

// Inherit Class A
class B : A
{
    // Create Object for class A
    A obj = new A();
    B()
    {
        // You can call method2
        obj.method2();
        // Error: You cannot call method1 Since Explicit implementation
        //obj.method1();
        // But you can access it by Interface Reference
        ((ISample)obj).method1();
    }
}

Finally, one more point, explicitly implemented interfaces' methods are not listed down in the Visual Studio IDE's intellisense.

Image 1

Readers, do you have any thoughts/opinions, please write your comments below!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Hi All, I am Francis from India. Working as a Senior Software Engineer. I have 6+ Years of experience in ASP.Net and C#/VB.Net. Also founder of ASPDotNetChamp, where i blog my day to day ASP.Net Experiences, Problems and Interview Questions.

Comments and Discussions

 
QuestionAre name collisions the only reason? Pin
cjb1101-Jun-14 21:11
cjb1101-Jun-14 21:11 
AnswerRe: Are name collisions the only reason? Pin
Francis S Michael2-Jun-14 0:17
professionalFrancis S Michael2-Jun-14 0:17 
GeneralRe: Are name collisions the only reason? Pin
Menci Lucio2-Jun-14 23:34
professionalMenci Lucio2-Jun-14 23:34 
In VB you MUST avoid name collision:
VB
Public Sub method1_a() Implements iSampe.method1
  [...]
End Sub
Public Sub method1_b() Implements iAnotherSample.method1
  [...]
End Sub


And call all of them:
VB
obj.method1_a()
obj.method1_b()
ctype(obj, iSampe).method1()

Do you know why they don't did the same for C#?
Is one of the things that make me prefer VB instead of C# (and there are some that make me prefer C# instead of VB, and I mixed solution, often it's impossible to have mixed projects... Aaaaaarrrrgghhhhhh!!!!)
Lucio Menci
Perché un poco sì, ma anche un poco no

GeneralThanks.. Pin
RhishikeshLathe1-Jun-14 20:33
professionalRhishikeshLathe1-Jun-14 20:33 
GeneralRe: Thanks.. Pin
Francis S Michael1-Jun-14 20:57
professionalFrancis S Michael1-Jun-14 20:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.