Tony said:
Hello!
I just wonder what is the reason for using public in this case
interface ITest
{
void Foo();
}
class Test : ITest
{
public void Foo() {...}
}
but not in this
class Test : ITest
{
void ITest.Foo(){...}
}
It's not entirely clear what you're asking. The literal answer to your
question is "because the C# specification requires 'public' in one, and
prohibits 'public' in the other".
From your later reply it seems you may be looking for an answer to a
different question, one you didn't actually ask. That is, "why does one
implement an interface implicitly rather than explicitly, or vice a versa?"
There are a variety of reasons one might make that choice one way or the
other, but they all come down to the basic difference in behavior
between the two: with implicit implementation, you can use the interface
member without casting to the interface, but with explicit
implementation you must cast to the interface.
IME, one of the more common reasons to use explicit implementation is
when you have a class that implements more than one interface, one of
which has a member that is the same name as a member in the other
interface. Explicit implementation is the only way to allow the
ambiguity to be resolved.
For example, implementing both IEnumerable, and IEnumerable<T>, each of
which has a GetEnumerator() method. Generally, one would implement
IEnumerable<T> implicitly, and IEnumerable explicitly.
Pete