Possible Duplicate:
Why can I only access static members from a static function?
While I was trying to call a normal method from inside a static method I got the error:
An object reference is required for the non-static field, method, or property
So this means I need to create the object of the Class and then call the nonstatic method. If I want to call the method directly then I have to declare that method as Static.
But , in this scenario , the calling method and called method belong to same class. So Why do I need to create an object while calling from a Static Method , while I can call a non-static method from non static method.
Ex:
class Program
{
//public void outTestMethod(int x,out int y)
//{
// y = x;
//}
static void Main(string[] args)
{
int a = 10;
int b = 100;
outTestMethod(a,out b);
}
private void outTestMethod(int x, out int y)
{
y = x;
}
}
Error:An object reference is required for the non-static field, method, or property
Static methods can call instance methods – but you need to have an instance on which to call them. It doesn’t matter where that instance comes from particularly, so for example:
Instance methods are associated with a particular instance of the type, whereas static methods are associated with the overall type instead – and the same is true for other kinds of members. So to call an instance method, you need to know which instance you’re interested in. For example, it would be meaningless to have:
because you need to know which person you’re talking about:
… that makes much more sense.
Typically instance methods use or modify the state of the instance.