Its a well known fact that a static method can work only on static members.
public static void Main()
{
Test t1 = new Test();
}
Here the Main
method is static, but I haven't declared t1
as static. Is it implicitly static?
No, it's a local variable. Local variables behave the same way whether they're declared in static methods or instance methods.
As a very rough guide (captured variables etc introduce complications):
Its a well known fact that a static method can work only on static members
This is not a fact; this is a falsehood. There is no restriction whatsoever; static methods have full access to all members of their type:
class C
{
private int x;
static C Factory()
{
C c = new C();
c.x = 123;
}
}
Factory is a static method; it has access to the private instance members of any instance of C.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With