Search code examples
c#classstatic-methods

Why can't you call a non-static method from a static method?


I have a static method [Method1] in my class, which calls another method [Method2] in the same class and is not a static method. But this is a no-no. I get this error:

An object reference is required for the non-static field, method, or property "ClassName.MethodName()"

Can someone please briefly describe why? including other things that might be related to this.


Solution

  • A non-static method requires an instance of the class. Unless you have passed in an instance, or created an instance in your method, you cannot call a non-static method, as you have no idea what instance of the class that method should operate on.