I Created and implemented an interface explicitly as below.
public interface IA
{
void Print();
}
public class C : IA
{
void IA.Print()
{
Console.WriteLine("Print method invoked");
}
}
and Then executed following Main method
public class Program
{
public static void Main()
{
IA c = new C();
C c1 = new C();
foreach (var methodInfo in c.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance))
{
if (methodInfo.Name == "ConsoleApplication1.IA.Print")
{
if (methodInfo.IsPrivate)
{
Console.WriteLine("Print method is private");
}
}
}
c.Print();
}
}
and the result I got on console is:
Print method is private
Print method invoked
So my question is why this private method got executed from other class?
As far as I understand the accessibility of private member is restricted to its declaring type then why does it behave so strangely.