Keyword modifier static in a method header, indicate that the method can be called without referring to an object
Anyone familiar with above statement please explain and support with a example program.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Posted Nov 26, 2011, 5:30 PM
VulpesPosted Nov 26, 2011, 1:51 PM
Static methods know nothing about particular instances - they apply to the class as a whole and so don't need an instance reference to call them.
Posted Nov 26, 2011, 1:17 PM
Satyapriya NayakPosted Nov 26, 2011, 12:32 PM
When a method declaration includes a static modifier, that method is said to be a static method.Static methods are called without an instance reference
A static method does not operate on a specific instance, and it is a compile-time error to refer to this in a static method.
Ex:-
class Program
{
public static int myVar; //a static field
static void Main()
{
myVar = 10;
Console.WriteLine(myVar);
Console.ReadKey();
}
}
In the above we don't have an object of the class to use that field since the field is static.
Refer
http://www.dotnetperls.com/static-method
Thanks