Some understanding of static state in C

Time:2021-4-18

1. Static and intuitive characteristics

One of the most remarkable characteristics of static members is that their scope is global. As long as the corresponding namespace is introduced at the call, we can use it anywhere in the code. We can consider using static for everything with global characteristics. In actual development, we often use static fields to share data, modify them as static methods, and use them as common tools.

 

2. Thinking on naming

From the opposite of the name of static, we can think of a word called dynamic, which can be metaphorized as instance members. Instance members can be described as dynamic because their creation and release of resources in memory are based on instantiated objects or GC(garbage collection)It’s changing. Static members are rarely changed in memory. Static members are created when the corresponding type is accessed for the first time, and resources are released at the end of the application.

 

3. The comparison between static and instance (non static)

static state

Instance (non static)

needstatickeyword

You don’t need statickeyword

Cannot instantiate. Can only be called with class name

The object must be instantiated before it can be called

In static methods, you can accessStatic members

In the instance method, you can access theStatic members

Not directly accessible in static methodsInstance memberTo access, you need to create an object

In the instance method, it can be accessed directlyInstance member

Initialize before calling (before creating the first instance or referencing any static member)

Instantiation(Call constructor)Object

 

4. The creation process of static member initialization (the focus of this paper)

When a class contains static members, how to initialize the static members inside the program?

4.1Code example1

As shown in the figure above, in this case, even when the program is running, the static members in the class are not created(Initialized). The reason for this is that the type corresponding to the static member in the program does not have the code to access the type anywhere. Static members are created when the type is accessed for the first time.

 

4.2Code example2

After following the principle of creating static members, this diagram changes the code to include access to types. Then static members will be created at this time.

From the visual code point of view, it is obvious that there will be a misunderstanding about the initialization of static members, because the static members in the code have been initialized and assigned values at the time of declaration. In fact, this way is just a kind of grammar sugar, is to facilitate the programmer to give a simple grammar. In fact, the assignment of fields inside a type is done by the constructor.

  Now let’s think about one question:No constructor is written in the above code, so how do static members initialize and assign values?

Through the general debugging, we can not see the internal execution process of the program, and it is difficult to find the answer to the problem. In order to explore the above problems, I can use the decompiler tool to view the decompiled code in the figure below. As shown in the figure:

In the figure above, we can see that MyClass is decompiledIt contains a static constructor and assigns values to static members. A conclusion can be drawn here: when defining a static member in a class, a static constructor is actually defined.

  

In the case of decompile code, we debug the static constructor displayed in the code to verify whether the static constructor is invoked, and then observe the order of the initialization of instance members and static members. As shown in the figure:

  

According to the demonstration in the figure above, we can see that the static constructor is called, and the static constructor will be called first, and then the instance constructor will be called.

 

5. Some important points about static constructor

  1. On the definition of static constructor: there must be no access modifier and no parameter. There can only be one constructor in a class or structure. Overloading and inheritance are not allowed.
  2. Static constructor call: static constructor can not be called directly like instance constructor when writing code, its call is made by common language runtime(CLR)Call. And the static constructor is called only once.

The following code diagram verifies that the static constructor will only be called once:

 

Suggestions for use:

Use static members as little as possible, because the life cycle of static members is released after the end of the application, howeverGC(garbage collection) there is no memory collection management for it. Make the meeting time occupation of memory, and will produce some memory garbage.

Thoughts on learning:

It seems that the use of simple static characteristics, some people use the design of a software pattern singleton pattern. It’s never too late to refine solid basic skills. I think the most difficult part of programming is to use it. Even if I learn to use high-end technology in combination, I still can’t really master it.