Welcome to AspAdvice Sign in | Join | Help

Orcs Goblins and .NET

I enjoy reading and writing. I hope you enjoy at least the former.

Explicitly and Implicitly Implementing Interfaces

I read an interesting blog post from Joydip Kanjilal where he described an interesting little trick with interfaces. I, being a bit of a fan of interfaces, read the post and thought I'd throw my $0.02 in also. I couldn't pass up an opportunity to talk about interfaces. He first shows simply how to create an interface and how to implement the interface implicitly. The difference between implicitly and explicitly writing this code is what creates the different circumstances Joydip shows in his demonstration.

I'll start where he did with an interface. I'll name my interface IBloggable and I'll have a class PostContent which implements the interface.

public interface IBloggable
{
    void SendToBlog();
}

public class PostContent : IBloggable
{
    public void SendToBlog()
    {
        // Send this content to a blog
    }
}

Ok so this is the implicit implementation of the interface. Basically all I mean when I say explicitly and implicitly is whether or not you're going to define everything. If I define something explicitly I have defined everything and made it perfectly clear. I have included every detail required for precise interpretation. Notice above I have not specified to which class that method applies. So here I will now define the same thing explicitly and make sure that the SendToBlog method specifies to what it belongs.

public class PostContent
{
    void IBloggable.SendToBlog()
    {
        // Send to blog only for the interface not for implementing classes
    }
}

As Joydip pointed out so well, the first one will work in either of these circumstances, but our explicitly defined method will only work in the first example.

Example 1:

IBloggable b = new PostContent();

Example 2:

PostContent b = new PostContent();

From this we can note a couple of other interesting points here.

If we implement interface methods implicitly, we are able to use the method with objects of the interface type or of the implementing class type. If we are implementing explicitly, the method will only work on the interface. The great benefit of only allowing instances of the interface to use the method is that it encourages you to write more dynamic and maintainable code since you'll be using the interface everywhere you'll be able to switch your implementing classes quickly and easily.

This is one of many great ways to write better code. I always explicitly implement my interfaces. I've not had it bite me yet, so if anyone knows a reason to not explicitly define this code, please let me know.

One nice feature in Visual Studio which makes this nice and easy. Once you specify the interface you want your class to implement you are able to right click on it and choose Implement Interface > Implement Interface Explicitly and it will create the stubs for everything required in order to implement the interface. It will even place it all in a nice code region for you. Observe the code it generates for us in this instance.

public class PostContent : IBloggable
{
    #region IBloggable Members

    void IBloggable.SendToBlog()
    {
        throw new Exception("The method or operation is not implemented.");
    }

    #endregion
}

Happy coding.

Sponsor

Published Friday, January 04, 2008 4:26 PM by Brendan

Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Explicitly and Implicitly Implementing Interfaces @ Saturday, January 05, 2008 2:43 AM

Excellent explanation!

Thanks,

Joydip

Author: ASP.NET Data Presentation Controls Essentials (Packt Publishing)

Joydip Kanjilal

# re: Explicitly and Implicitly Implementing Interfaces @ Tuesday, January 08, 2008 2:41 AM

Explicit interface implementation is something I use all the time and it's very useful used in the right way. If you code for your self always using it would be fine, but if your making code that's to be shared, for example some kind of api or class library I'm sure you'd be hanged if implementing all interfaces explicitly. It makes classes a lot more difficult to understand and also it hides the implemented members from intellisense if an instance is not cast to the interface. The notion of always programming against interfaces is good and something you definitely should strive to do, but in the case of an object implementing more than one interface you'd have to do runtime casts to get to members belonging to different interfaces on the same object.

I believe that explicit implementation should be limited to members that makes no sense when the runtime type of an object is known, for example if you implement your own "LinkedList"-class which implements the IList-interface, you'd explicitly implement the IsReadOnly-property since there's no reason to ask if your instance is read only once you know it's a LinkedList and you didn't implement it as a read only list.

Patrik

# re: Explicitly and Implicitly Implementing Interfaces @ Tuesday, January 08, 2008 9:17 AM

Patrik,

Thank you. Yes, I like your suggestion here. I definitely agree that if using multiple interfaces it is a good idea to not implement the interfaces explicitly.

You're also correct about for writing APIs since it is not internal code, and you don't want to impede the styles of other people's coding. I believe it is a best practice reserved for internal code.

Thanks for the great comment!

Brendan

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below