C # static variables summary

In C # program, there is no concept of global variables, which means that all instances of the class member variables can only manipulate the data, which has played an "information hiding" role. But sometimes, this is not a wise choice.

Suppose we want to define a class library, the number of required class can save the book, i.e., each additional book (a defined instance), a number of books should be added. If there are no static variables, we need the number of books stored in each book (example), the However, such variables to be stored repeatedly in different books (instances) in Books (example) a small number of words, we can accept, If more books (instances) the number of words, such as tens of thousands, we can not imagine how much waste of resources (such as memory, disk space) to create more intolerable is: because the number of books (instances) to saved in each book (example) where the value is certainly different. For these same as the number of stored books (instance) Books (example), we have to increase each time a new book (create a new instance), change the value stored in all other books (instance). Oh, My God! You will yearn to re-process-oriented programming method, longing for the times we have global variables. However, this situation will not arise, because C #, you ready for another variable types: static variables. It is similar to the global variable in a class, the class information stored in public, all class instances (objects) that share value.

The syntax for declaring static variables are as follows:

<Access modifier> static data type variable name;

this access modifier with other members of the class, as can be public, protected, private, or such as internal.

Before ordinary member variables plus the static keyword, it becomes a static variable. Static variables and instance variables are stored in different ways, static variables declared in the beginning, has been stored in memory until the entire program is terminated. The instance members to allocate memory when you create an object and store a copy of the data. So static member variables considered to belong to the class, not the object.

The difference between static variables and instance variables are as follows: 
1. Memory allocation 
static variables when the application is initialized, it is in memory of them, until the end of the program to run until its demise in the class; 
instance variables need to be instantiated only after Allocate memory. 
2. The lifetime 
of static variable lifetime for the present cycle of the application; 
present instance variable cycle period dependent on the presence of instantiated class. 
3. Call the way 
. "Class static variable name" static variables can only be invoked instance of the class can not be called; 
instance variables when the class where the variable is instantiated, instantiate the class name directly accessible through. 
4. sharing 
static variables are global, shared by all instances of the object class, i.e., an example of changing the values of static variables, other examples of similar value is read after the change; 
instance variables are local, not shared of. 
The access method 
static members can not access instance members; 
instance members can access static members.

The memory static variables to allocate memory when the class is loaded, the object created later are used, the appropriate action is to this memory to operate. It can also be seen as an alternative global variables.

So: the value of static variable initialization will only only once, each time behind the visit, are the last processed value (even in an internal function). Every instance variables are initialized.

the System the using;
class Program
{
static void the Main (String [] args)
{
// static variable output undefined, the result is 0; also described, in C # without an initial value of the variable is automatically assigned 0
Console. the WriteLine (sort.i);
// access method static variables (class name static variable name.), but it can also be seen in the external static variables are not mysterious operation static variable;
sort.i = 5;
// output 5
Console.WriteLine (sort.i);
// constructor can also be assigned by the initial value of a static variable, huh, huh
sort sortTest = new sort ();
assignment // constructor output of 3;
Console.WriteLine (sort.i );
}
}

class sort
{
public static int i;
public sort()
{
i = 3;
}
}

Static variables must use the class name to reference, but can not use the instance of the class, because static variables do not belong to any instance, but common. We can draw an analogy: in a class, some items are personal, we want to use, the owner of the goods must be pointed out, for example, "Wang San bike" program in C #, we can use: the king three bicycles format. Some of the items are common to all objects, can not use his own name, but the use of the name of the class, such as class group of investors to buy basketball, only said: "The class of basketball," We can not say: "King III basketball." . This is definitely not enough, this is definitely not fair to others, we can think of many corrupt officials is the use of things that are not their own, or an individual use of public things and ruin themselves.

Useful say is this: static variable is the class name to reference it. Namely: the name of the class static variable name;

One concrete example is as follows:

using System;
using System.Collections.Generic;
using System.Text;

class Program

{

class Class1
{

public static String staticStr = "Class";
public String notstaticStr = "Obj";

}

static void Main(string[]args)

{

// static variable accessed through class, all instances of same static variables are the same value

Console.WriteLine("Class1's staticStr: {0}", Class1.staticStr);

Class1 tmpObj1 = new Class1();

tmpObj1.notstaticStr = "tmpObj1";

Class1 tmpObj2 = new Class1();

tmpObj2.notstaticStr = "tmpObj2";

// non-static variables are accessed through the object, the same non-static variables of different objects can have different values

Console.WriteLine("tmpObj1's notstaticStr: {0}", tmpObj1.notstaticStr);

Console.WriteLine("tmpObj2's notstaticStr: {0}", tmpObj2.notstaticStr);

Console.ReadLine();

}

}

Guess you like

Origin www.cnblogs.com/muxueyuan/p/11094745.html
Recommended