views:

183

answers:

5

I came across some interesting C# syntax that I'm not familiar with in the source code for the Composite Application Library for WPF's DelegateCommand<T> class.

There are some method declarations which are prefixed with the ICommand interface name, and they do not have accessibility modifiers specified. For example:

bool ICommand.CanExecute(object parameter) { ... }

What is this syntax called and where can I read more about it? I assume there's an implicit public, but I can't figure out what the benefit of specifying the class name is. My guess is that it might just be there for organization.

+5  A: 

When you place a method like this, you're saying that this is the explicit implementation of the interface. You can read a good tutorial on MSDN via that link.

Also, a comparison might be helpful for a full view of what this means.

Nick Craver
+1  A: 

It is called explicit implementation of interfaces. What it means is that this particular implementation of CanExecute will not be visible (and will not run) UNLESS the object is cast as an ICommand.

This can be useful in allowing a class to provide different implementations for different interfaces where method names overlap

public interface  InterfaceOne {
  void SomeMethod();
}
public interface InterfaceTwo {
  void SomeMethod();
}
public class Impl : InterfaceOne, InterfaceTwo {
  public void InterfaceOne.SomeMethod() {Console.WriteLine("One");}
  public void InterfaceTwo.SomeMethod() {Console.WriteLine("Two");}
}

I personally hate this this syntax. Take the example of OracleParameter which provides only an explicit implementation of ICloneable.

If you have a reference to OracleParameter, the Clone() method will not appear in intellisense and will not be visible when you "Go To Definition". However the ability IS there if you do ((ICloneable)parameter).Clone(). In order to even know that this is possible, you can do that you are pretty much stuck googling around in blogs.

George Mauer
+4  A: 

It's termed Explicit Interface Implementation:

If a class implements two interfaces that contain a member with the same signature, then implementing that member on the class will cause both interfaces to use that member as their implementation.

If the two interface members do not perform the same function, however, this can lead to an incorrect implementation of one or both of the interfaces. It is possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interface. This is accomplished by naming the class member with the name of the interface and a period.

Explicit Interface Implementation Tutorial

Mitch Wheat
+1  A: 

This is called explicit interface implementation and you can read about it here.

The basic idea is that those methods/properties are only accessable when explicitly used via an interface instance of that type.

Frank Bollack
A: 

This is called explicit interface implementation and you can read more about it here.

In Visual Studio, if you inherit from an interface, you can right-click on the interface name in your class definition. You can click either "Implement Interface", or in the submenu, "Implement Interface Explicitly". This is a handy shortcut for implementing interfaces.

Andy West