The answer from Philipp is correct; I'd like to extend it slightly by addressing directly your statement:
somehow I’m still able to call it ... how can that be if I marked it as private?
The question indicates an important but subtle misunderstanding of what "private" applies to. "Private" does NOT mean "cannot be called from outside". Consider:
class C
{
private static void M() { whatever }
public static Action A() { return M; }
}
Any code can call M()
by invoking C.A()()
. Notice that in this scenario the call to M is made from code outside C; there is no call to M()
inside C
, merely returning a delegate to it.
And as Philipp's answer notes, there are other ways that code can access C.M
, such as having been granted sufficient permissions to do private reflection.
What then does private
mean, if not "this code can only be called from inside C
?"
private
means the name of the thing declared may only be legally referred to by an identifier within the accessibility domain of a private member. That is, use of the identifier M
to refer to the private member C.M
is only legal inside the accessibility domain of C.M
.
The private accessibility domain for a method of a class is the region of program text that falls within the outer { }
braces for the class (including partial class declarations and nested classes).
The thing that I'm emphasizing here is that private
gives a restriction on the source code of a legal program. It says that the actual program text M
may only be used to refer to member C.M
if that program text appears somewhere inside the braces of the class declaration. An attempt to use the program text M
outside of the accessibility domain will result in either binding to a different thing named M
, or an error.
But the key thing to understand here is that private
is all about the legality of the text of the program's source code. It is not a fact about behaviour at runtime! You can still call a private member; you just can't do it by writing source code that uses identifier M
outside of the declaration of C
.