When you explicitly implement the interface, you first have to cast the object to the interface, then you can call the method. In other words, the method is only available when the method is invoked on the object as the interface type, not as the concrete type.
class Vehicle: IVehicle {
public int IVehicle.getWheel()
{
return wheel;
}
public void printWheel()
{
Console.WriteLine( ((IVehicle)this).getWheel() );
}
}
See this reference at MSDN for more information. Here's the relevant snippet:
It is not possible to access an explicit interface member implementation through its fully qualified name in a method invocation, property access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.
For what it's worth -- this probably isn't a particularly good use of explicit interface implementation. Typically, you want to use explicit implementation when you have a class that has a full interface for typical operations but also implements an interface that may supersede some of those operations. The canonical example is a File
class that implements IDisposable
. It would have a Close()
method but be required to implement Dispose()
. When treating as a File
you would use Open/Close
. When opened in a using statement, however, it will treat it as an IDisposable
and call Dispose
. In this case Dispose
simply calls Close
. You wouldn't necessarily want to expose Dispose
as part of the File
implementation since the same behavior is available from Close
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…