If the method iAmPrivate is Private then how does the following execute it? If I try to run PrivateMethod’s myPrivateMethod it is encapsulated why doesn’t the same apply to iAmPrivate ?
class PrivateMethod {
private int myPrivateMethod() {
return 1;
}
}
static void Main(string[] args) {
Program myProgram = new Program();
myProgram.iAmPrivate("private");
myProgram.iAmPublic("public");
PrivateMethod pm = new PrivateMethod();
//Console.WriteLine("this won't run {0}", pm.myPrivateMethod); //not possible
Console.WriteLine("press [enter] to exit");
Console.ReadLine();
}
public void iAmPublic(string s) {
Console.WriteLine("I am {0}", s);
}
private void iAmPrivate(string s) {
Console.WriteLine("I am {0}", s);
}
[Thanks for the help… Now I get it!]
Changing the class
PrivateMethodto the following helped me understand what is going on:The fact that I was missing is that
myProgram.iAmPrivate("private");was being run from withinMainso of course it is accessible.