tags:

views:

101

answers:

5

I saw this code and have some questions:

public class MyClassB : MyClassA, IMyClass
{
     void IMyClass.InsertUser(User user) { }
}

Why did they have to prefix the interface, IMyClass and not just do:

void InsertUser(User user) { }

or maybe do

void new InsertUser(User user)

Also, since you are inheriting from MyClassA (which implements the interface IMyClass), why do we see:

public class MyClassB : MyClassA, IMyClass { }
+10  A: 

Just

void InsertUser(User user)

wouldn't work as that would declare a private method. The version with the prefix is called explicit interface implementation. It makes the method available only via the interface. One of its main uses is to allow you to implement multiple interfaces which contain the same method. The best example of this is IEnumerable<T> which extends IEnumerable - both have a GetEnumerator() method, but with different return types. You'd typically do:

public class Foo : IEnumerable<T>
{
    // Implicit interface implementation
    public IEnumerator<T> GetEnumerator()
    {
        // Real work
    }

    // Explicit interface implementation
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator(); // Calls method above
    }
}

As for redeclaring the interface, this is necessary if you're also reimplementing the interface which it looks like you're doing here. My guess is that MyClassA implements the interface with a non-virtual method (or explicitly), which means it can't be overridden in the derived class... so to reimplement the interface, you've got to redeclare it.

Personally I'd try to avoid this situation where possible - it can lead to all kinds of problems when the method that gets called depends on exactly how you happen to be "looking at" the object in question. It would be better for MyClassA to implement the interface with a virtual method which MyClassB could then override.

Jon Skeet
so the method in MyClassA should be marked virtual, and then I just override it? That is what I am used to doing!
mrblah
A: 

It's an explicit interface implementation.

Most likely it's that way because the code let the VS wizard to it for him/her. It's a feature to allow disambiguation of methods with otherwise similar signatures.

plinth
A: 

The prefix means that the interface has been explicitly implemented. When you have an instance of your class you won't be able to call the method directly, instead you'll have to cast the instance to the interface in order to call the method.

There's two reason you'd do this. This first is then you're implementing multiple interfaces that have the same method name but need to behave in different ways. The second is when you need to implement an interface but don't want the methods to be generally available to the user unless they explicitly want them

Sean
+1  A: 

Your first question is already answered. Here's a go at the others:

void new InsertUser(User user)

This would create a new implementation of InsertUser that would hide the one in the base class. So:

myClassBInstance.InsertUser(user);

would call MyClassB.InsertUser(), whereas

MyClassA myClassAInstance = myClassBInstance;
myClassAInstance.InsertUser(user);

would call MyClassA.InsertUser().

This is in contrast to the explicit interface implementation, which will only be available through the interface. Then

IMyClassA myIClassAInstance = myClassBInstance;
myIClassAInstance.InsertUser(user);

would call MyClassB.InsertUser, whereas:

myClassBInstance.InsertUser(user);

...would call MyClassA.InsertUser().

Now, for the last question: If MyClassA implements IMyClass, the declaration:

public class MyClassB : MyClassA, IMyClass { }

is actually redundant (but legal). As you suppose, MyClassB inherits the interface from MyClassA. So the declaration

public class MyClassB : MyClassA { }

would be equivalent.

Tor Haugen
A: 

The reason you have to type the interface name or new it up is that the MyClassA also uses IMyClass interface which means it implements the InsertCustomer method.

azamsharp