Hello Folks,
I was exploring the explicit interface implementation and got a question.
In the example below public void ITest.TestMethod() gives error however void ITest.TestMethod() works.
May I know the reason?
interface ITest
{
void TestMethod();
}
class TestClass: ITest
{
//public void ITest.TestMethod() //Error The modifier 'public' is not valid for this item
void ITest.TestMethod() //Works
{
Console.WriteLine("Explicit Interface Implementation");
}
}


Guest UserRank #816 · 2.8K reputationPosted Apr 22, 2015, 12:47 AM EDT
Quote: "An interface member that is explicitly implemented cannot be accessed from a class instance:"
Because you use explicit implementation, you cannot give it an access specifier, the method can only be called by using the interface, not the class instance.
Prakash TripathiRank #72 · 25.3K reputationPosted Apr 24, 2015, 9:24 AM EDT
Guest UserRank #816 · 2.8K reputationPosted Apr 24, 2015, 11:33 AM EDT