Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why explicit interface implementation?

Tags:

c#

interface

I recently implemented a class like:

class TestClass : IDisposable
{
    RegistryKey m_key;
    public TestClass()
    {
        m_key = Registry.CurrentUser.OpenSubKey("Software", false);
    }

    public void Dispose()
    {
        // m_key.Dispose();
        IDisposable disp = m_key;
        disp.Dispose();
    }
}

If I uncomment the direct call to Dispose, I get error CS0117 ("'Microsoft.Win32.RegistryKey' does not contain a definition for 'Dispose'"). Some Googling led me to this thread, where I learned what was going on, so I now understand the mechanics of it. The MSDN documentation suggests that the author would prefer that I call Close() instead of Dispose(), but doesn't explain why.

What is the purpose of this pattern (which I think I've seen it in the IO classes as well)? In light of the fact that this was an intentional decision by the class author, how bad is the code above (the call to Dispose through the IDisposable interface)? It can't be too bad - after all, it's what would happen in a using statement, right?

[edits: 1) changed title from "non-public" to "explicit" 2) removed the explicit implementation from my code, accidentally left in from experimentation]

like image 832
bmm6o Avatar asked Jan 03 '09 00:01

bmm6o


People also ask

What is the use of explicit interface?

What is Explicit Interface Implementation and when is it used? Explicitly telling the compiler that a particular member belongs to that particular interface is called Explicit interface implementation.

What is implicit and explicit interface implementation in C#?

An implicit interface implementation is where you have a method with the same signature of the interface. An explicit interface implementation is where you explicitly declare which interface the method belongs to.

How do you call an explicit interface?

The following is an example of how to call "Explicit Interface Method" in the same class using class methods. Output: Class1 Display Method. Iinterface_1 Method Explicit interface implementation. Iinterface_1 Method Implicit interface implementation.

Can I declare any of the members of an interface Public explicitly why?

Interface is a contract and anywhere where you can access the interface, you should be able to access all the methods in it. In other words, all the methods declared in the interface are supposed to be public so it doesn't make sense stating it explicitly.


1 Answers

This is called explicit interface implementation. In your example since you define the Dispose() method as "void IDisposable.Dispose()" you are explicitly implementing the IDisposable interface as well.

This is normally done to avoid collisions. If Microsoft ever wanted to add another Dispose() method that did something else to RegistryKey they wouldn't be able to unless they used explicit implementation of that interface.

This is done often with the generic IEnumerable<T> interface. It requires you to also implement the non-generic interface IEnumerable. The only member in these two interfaces is GetEnumerator, with the generic one being more useful, so its usually implemented like this:

public clas SomeClass : IEnumerable<SomeOtherClass>
{
    public IEnumerator<SomeOtherClass> GetEnumerator ()
    {
        ...
    }

    IEnumerator IEnumerable.GetEnumerator ()
    {
        return GetEnumerator ();
    }
}

This way when you call an object of SomeClass's GetEnumator method, it calls the generic version, since the other one was implemented explicitly, allowing us to get the strong-typing generics allow.

See pages 166-169 of Programming C# by Jesse Liberty (I've got the fourth edition).

like image 155
Mark A. Nicolosi Avatar answered Oct 21 '22 08:10

Mark A. Nicolosi