Static and non-static variables
It is static in C.
In C #, an object application is required for any non-static member in the class.
Class
{
Static int;
Int B;
}
Main
{
Int I = 0;
A classa = new ();
// To use B, you must write it
Classa. B = I;
// To use a, you must write it
A. A = I;
// No, classa is an object, and A is a class;
}
Theoretical part:
I. Basic concepts of static variables and non-static variables
Data members include static variables and non-static variables.
A static variable: a variable with a static modifier is called a static variable.
B Non-declarative variables: variables without static modifiers are called instance variables.
2. differences between the two
class test
{< br> static int B = 0; // static variable
int A = 0; // non-static variable
}< br> 1. memory Allocation difference: static variables are defined in the memory, it will always exist until the Program of its class stops running; instead, non-static variables need to be instantiated before memory is allocated. The existence cycle of a non-static variable depends on the life cycle of the instantiated class.
so their call methods are also different:
static variables: You can (only) use the class itself. static variable names, and class instances cannot be called.
non-static variable: when the class of the variable is instantiated, it can be accessed by the instantiated class name.
(In addition, static members cannot access non-static members; non-static members can access static members)
test. B = 2; // OK
test T = new test ();
T. a = 0; // OK
T. B = 3; // error occurs when the error code is compiled. You cannot use instance reference to access static members.
2. share the values of the two: static variables are shared by all class instance objects, that is, the value of static variables is changed for one instance, and other similar instances read the changed values. Non-static variables are not shared.
ex: the final output result below is: 10, 0
class class1
{< br> Public static int CS1 = 0;
Public int CS3 = 0;
}< br> class class2
{< br> Public void a ()
{< br> console. writeline (class1.cs1. tostring ();
// Because CS1 is a static variable, when the class program is modified to CS1 = 10, the value read by instances of other classes is the modified value 10, so the output here is: 10
Class1 cc = new class1 ();
Console. writeline (CC. cs3.tostring ());
// Although the class program value is modified to CS3 = 34, because CS3 is a non-static variable in the class1 class, its life cycle is the same as that of its class instantiated, therefore, the value of the class name CT instance does not exist in the memory after the instance ends. When the class name CC is used for instantiation, the variable CS3 is called, the value is still 0 when the non-static variable is initialized.
}
}
Class Program
{
Static void main (string [] ARGs)
{
Class1.cs1 = 10;
Class 1 Ct = new class1 ();
Ct. CS3 = 34;
Class 2 cm = new class2 ();
Cm. ();
}
}
Class Name. static variable name
Instantiation name. Non-static variable name