C# Compiler : cannot access static method in a non-static context

A call to a static method will be compiled to call a specific static method on a specific class. In other words, it won’t use the contents of B to determine which static method to call.

So the call has to be resolvable at compile time, hence it complains, because for all it knows, you could replace the contents of that property with multiple concrete types, which would mean that the call to the static method would have to be resolved to a static method in either of these classes.

The compiler does not have anything like a virtual or abstract static method, so for one you can’t guarantee that all of those classes have that static method. And since the call has to be resolvable at compile time, it won’t work like that.

You can, as you’ve noticed, call an instance method of the object, which in turn calls the static method. This does not invalidate the above rules since when the compiler compiles that instance method, which static method it will call is constant and known.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)