Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to use static variable and method in c#
Posted
Comments
Sandeep Mewara 13-Sep-10 6:18am    
If not MSDN... Google?

Hi,

http://msdn.microsoft.com/en-us/library/79b3xss3%28VS.80%29.aspx[^]

Refer this link it will helpful to you.

Cheers :)
 
Share this answer
 
use static keyword.

public static int MyStaticVariable;

public static void MyStaticMethod()
{
}


:)
 
Share this answer
 
Comments
Kunal Chowdhury «IN» 13-Sep-10 6:34am    
Good Answer.
Hi Sheetal,

Why don't you search on Google? You will find it over net very easily. Try to read any Book, which will answer your query.
 
Share this answer
 
I think what he meant was how do you use static variables, not how to declare them. But if I'm wrong and you are indeed asking how to declare them, then.
C#
public static int sv;
public static void sm()
{
}


You call static variables and method through the class name
MyClass.sm();
MyClass.sv = 90;


There is only one copy of the static variable per class during a single runtime. Hence, it is not attached to any object. Static variables are used as counters for how many objects of a class are dispatched or the common information to be shared among the objects of the same class or the constant of a class such as the PI Math constant.

If this is a school assignment, I hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900