"Kevin Frey" <ke**********@hotmail.com> wrote in message
news:e2**************@TK2MSFTNGP11.phx.gbl...
Consider this simple example:
interface IReader
{
bool Read( );
};
class MyReader : IReader
{
bool Read( ); // or should it be bool IReader.Read( ) ?
};
When I declare the implementation of the Read( ) method, what syntax
should I be using - the interface-qualified method name, or not.
I seem to have found situations where the interface-qualified name was not
permitted (can't remember the exact circumstances). It's a pity because
the interface-qualified names really improve the readability of classes
which implement interfaces.
Not really - when you qualify them like that it is called an explicit
interface member and is not unusable without casting to the interface.
There are 3 'classic' examples:
1) Subject specific naming or implementing 'implementation' interfaces
class MyFile: IDisposable
{
public void Close()
{
// you close files don't you
}
void IDisposable.Dispose()
{
Close();
}
}
using(MyFile f = new MyFile())
{
f.Dispose(); // ERROR
f.Close(); // OK
} // f.Dispose called here automatically
2) Implementing interfaces and typesafe alternatives of the same name
class MyFile : ICloneAble
{
// unhelpfully typed version required by interface
object ICloneable.Clone()
{
// call nice version
return Clone();
}
// nicely typed version
public MyFile Clone()
{
return ...;
}
}
3) implementing multiple interfaces with name clashes
interface I1 { void F(); }
interface I2 { void F(); }
class C : I1,I2
{
void I1.F() { /* do I1 thing */ }
void I2.F() { /* do another thing */ }
}