Implicit vs. Explicit Implementation of an Interface

Introduction

Have you ever wondered how to implement an interface and what the difference is between implementing it implicitly versus explicity? I did a lot of searching on the topic myself before really getting a firm grasp on the differences. Let me explain in this article how to go about using the two methods.

What is an Interface?

First let’s just review what an interface is. Microsoft defines an interface as “A reference type that defines a contract. Other types implement an interface to guarantee that they support certain operations. The interface specifies the members that must be supplied by classes or other interfaces that implement it.” In other words if an interface defines a method named GetUser which takes as input a string called “userName” and returns an instance of MembershipUser then any class that implements this interface must define a method called GetUser and has the same inputs and outputs as defined in the interface. That’s how the contract is established.

Implicitly Implemented

Implementing an interface using the implicit method is pretty straight forward. Let’s say we create an interface called "ImyInterface" which defines two members as such:

public interface IMyInterface

{

string ConnectionStringName { get; set; }

 

string GetStateName(string cityName);

}

As you can see this interface defines a property called "ConnectionStringName" which is of type "string." Both the get and set accessors are defined. It also defines a method called "GetStateName" which takes a string as input and returns a string. Now in order to implement this interface implicitly we would just define the two members in a class and set their scope as public.

public class MyInterfaceClass : IMyInterface

{

private string _ConnectionStringName;

 

public string ConnectionStringName

{

get { return _ConnectionStringName; }

set { _ConnectionStringName = value; }

}

 

public string GetStateName(string cityName)

{

return null;

}

}

This class would compile as-is. It implements the property correctly and also the method although I left out any logic to look up the value in the method. Honestly the interface doesn't care what logic is contained in the method or the property accessors. All it cares about is that the members are defined.

That's all it takes to implement using the implicit method. Just define the members that the interface requires. The advantage of implementing implicitly is that you can access the members through an interface variable or a class variable as such. You'll understand the difference in the next section.

public void SomeMethod()

{

// This is legal

IMyInterface i = new MyInterfaceClass();

string stateName = i.GetStateName("Raleigh");

 

// This is also legal

MyInterfaceClass j = new MyInterfaceClass();

string stateName1 = j.GetStateName("Raleigh");

}

 

Explicitly Implemented

So how do we implement the same interface explicitly? Well there's a couple changes we would need to make. First we don't define the scope of the member. It will automatically be made public. The second this is we need to prefix the name of the member with the interface name followed by a period. So with these changes our class definition will now look like this.

public class MyInterfaceClass : IMyInterface

{

private string _ConnectionStringName;

 

string IMyInterface.ConnectionStringName

{

get { return _ConnectionStringName; }

set { _ConnectionStringName = value; }

}

 

string IMyInterface.GetStateName(string cityName)

{

return null;

}

}

Now there's a couple things to consider. First how does this affect our method we declared earlier. Well for one thing the 2nd block of code won't be legal anymore. Take a look.

public void SomeMethod()

{

// This is legal

IMyInterface i = new MyInterfaceClass();

string stateName = i.GetStateName("Raleigh");

 

// This is NOT legal

MyInterfaceClass j = new MyInterfaceClass();

string stateName1 = j.GetStateName("Raleigh");

}

If you tried to do the 2nd block of code it would not compile. This is because when an interface is implemented explicitly you can only access the interface members of the class when you're accessing through an instance of the interface and not an instance of the class. Now what are the advantages of doing this way. Let's say you create a second interface that you want to implement in the same class and what if the second interface had a member exactly like the first one. How would they be differentiated?

public interface IMyOtherInterface

{

string GetStateName(string cityName);

}

 

public class MyInterfaceClass : IMyInterface, IMyOtherInterface

{

// Can't put this method twice as it won't compile

public string GetStateName(string cityName)

{

return null;

}

 

public string GetStateName(string cityName)

{

return null;

}

}

The previous code shows the dilemma. Two methods with the exact same footprint will not compile. So this is the advantage that explicitly implementing has. We can take two interfaces with a matching member and implement one or both of them explicitly and it will compile. Here's an example.

public class MyInterfaceClass : IMyInterface, IMyOtherInterface

{

public string GetStateName(string cityName)

{

return null;

}

 

string IMyOtherInterface.GetStateName(string cityName)

{

return null;

}

}

This class would compile and it clearly defines which method belongs to which interface.

Conclusion

So there you have it. The difference between explicit and implicit implementation and why you would choose one method over the other.

New Comment

  
  
  
  
Email Print