C # static variable static class static member

Source: Internet
Author: User

The differences between static variables and non-static variables are as follows:
1. Memory Allocation
Static variables exist in the memory during application initialization until the end of the program running of the class in which the static variables are stored;
Instead, non-static variables need to be instantiated before memory allocation.
2. Lifecycle
The life cycle of static variables is the application's life cycle;
The existence period of non-static variables depends on the existence period of the instantiated class.
3. Call Method
Static variables can only be called by "class. static variable name", but cannot be called by class instances;
Non-static variables can be directly accessed by the instantiated class name after the class where the variable is located is instantiated.
4. Sharing Mode
Static variables are global variables shared by all class instance objects, that is, the value of static variables is changed for one instance, and those of other similar instances read the changed values;
Non-static variables are local variables and are not shared.
5. Access Method
Static members cannot access non-static members;
Non-static members can access static members.

Static variables are allocated when the class is loaded. The memory will be used for all objects created later. The corresponding operation is to operate the memory. It can also be seen as an alternative global variable.

In webserivice, you only need to perform initialization once, instead of occupying a large amount of resources every time. You can also manage the called services. For example, if you want to queue the called services for execution, you can put the desired information in the arraylist and wait in line for execution in the static clock.

Next I will refer to an article on static variables.

C # static constructors and static variables

Static constructor:

(1) It is used to initialize static and read-only fields.

(2) You cannot add access modifiers when adding static keywords because static constructors are private.

(3) Static constructor of a class can be executed at most once in a given application domain: only the static constructor of the class is activated when an instance of the class is created or any static member of the referenced class is activated.

(4) Static constructors cannot be inherited and cannot be called directly.

(5) If the class contains the main method used to start execution, the static constructor of the class will be executed before calling the main method.

(6) Any static field with an initial value setting item, the initial values must be executed in the text order before executing the static constructor of this class.

(7) If a static constructor is not compiled and the class contains a static field with an initial value, the compiler automatically generates the default static constructor.

Classic example:

 

1 /************************************** ************
2 * static structure function training
3 * (1) ① ③ ...... Execution Order
4 * (2) output result: static ()
5 * Static B ()
6 * x = 1, y = 2
7 *************************************** ************/
8 using system;
9 Class
10 {
11 public static int X;
12
13 static A () // ④ return to ③ After execution
14 {
15X = B .y + 1;
16console. writeline ("static ()");
17}
18}
19
20 Class B
21 {
22 public static int y = a.x + 1; // ③ the static member of a is called,
23 // go to the static constructor of a ---->
24
25 static B () // ② If the static field with the Initial Value Setting item,
26 // when executing the static constructor of this class,
27 // perform the initial values in the text order first.
28 // go to the initial value setting item ---->
29 {
30console. writeline ("static B ()");
31}
32
33 static void main () // ① program entry,
34 // if the class contains the main method used to start execution,
35 // The static constructor of this class will be executed before the main method is called.
36 // convert to the static constructor of B ---->
37 {
38console. writeline ("x = {0}, y = {1}", a.x, B .y); // ⑤ output result
39console. Readline ();
40}
41}

 

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //

Some points about the C # static constructor indicate that the static constructor is a new feature of C #, which seems to be rarely used. However, we need to use static variables when initializing them. This constructor belongs to a class rather than an instance. That is to say, this constructor will only be executed once. That is, it is automatically called by. net before creating the first instance or referencing any static members.
Class simpleclass
{
// Static Constructor
Static simpleclass ()
{
//
}
}

Pay attention to the following points when using static constructors:

1. The static constructor neither has an access modifier nor a parameter. Because it is called by. net, modifiers such as public and private are meaningless.
  
2. When the first class instance or any static member is referenced ,. net will automatically call the static constructor to initialize the class. That is to say, we cannot directly call the static constructor, so we cannot control when to execute the static constructor.

3. A class can have only one static constructor.

4. A non-parameter constructor can coexist with a static constructor. Although the parameter list is the same, one belongs to the class and the other belongs to the instance, so there is no conflict.

5. Run only once at most.

6. Static constructors cannot be inherited.

7. If no static constructor is written and the class contains static members with an initial value, the compiler automatically generates the default static constructor.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.