what is the use of static variable in c#?When to use it?why cant i declare the static variable inside Method?
static variables are used when only one copy of the variable is required. so if you declare variable inside the method there is no use of such vaiable its become local to function only..
example of static is
Variables declared static are commonly shared across all instances of a class. When you create multiple instances of VariableTest class This variable permament is shared across all of them. Thus, at any given point of time, there will be only one string value contained in the permament variable.
Since there is only one copy of the variable available for all instances, the code this.permament will result in compilation errors because it can be recalled that this.variablename refers to the instance variable name. Thus, static variables are to be accessed directly, as indicated in the code.
example of static is
class myclass
{
public static int a = 0;}
Variables declared static are commonly shared across all instances of a class.Variables declared static are commonly shared across all instances of a class. When you create multiple instances of VariableTest class This variable permament is shared across all of them. Thus, at any given point of time, there will be only one string value contained in the permament variable.
Since there is only one copy of the variable available for all instances, the code this.permament will result in compilation errors because it can be recalled that this.variablename refers to the instance variable name. Thus, static variables are to be accessed directly, as indicated in the code.
Comments
Post a Comment