Analysis of static variable usage in C class

  • 2020-12-13 19:04:26
  • OfStack

This article illustrates the use of the static variable in the C# class. Share to everybody for everybody reference. The specific analysis is as follows:

Let's start with a snippet of code:

using System; 
namespace Param
{
class Class1
{
static int i = getNum();
int j = getNum();
static int num = 1;
static int getNum()
{
return num;
}
[STAThread]
static void Main(string[] args)
{
Console.WriteLine(string.Format("i={0}",i)); Console.WriteLine(string.Format("j={0}",new Class1().j)); Console.Read(); }
}
}

The above code is a netizen told me, let me see the result, I looked at one eye immediately said the result is
i = 1
j = 1
Netizen runs after saying the result is
i = 0
j = 1
I was frightened 1 jump, looked at the code carefully, originally oneself really wrong.

Now let me explain to you why this is the latter

For a class, static variables are object-independent, so when are they initialized? At the first citation. When a class generates an object, there are three simple steps

1. Allocate memory for all static variables, where the value in memory is the default value of the variable type. Check out the default values for different value types, and note that value types and reference types are different.
2. Assign values to static variables, also note that the value type and reference type are different.
3. Generate the object, call the constructor, first call the constructor of the parent class of the class, then call the constructor of the class itself, generate the object.

Now analyze the above code:

Console.WriteLine(string.Format("i={0}",i)); Here i is the static variable, and class class1 is cited for the first time. Follow the three steps above to allocate memory for all static variables in class1. In spite of the hyper-threading technology, instructions are executed in logical sequence one by one, so first allocate memory for static int i and keep the default value of int 0 in that memory, and then allocate memory for the variable static int num, which of course also has a value of 0.

Then perform step 2 to assign the variable: first assign the variable static int i = getNum (). Look at the code in getNum, that is return num. At this time, the value of num is 0, so i is 0. Then assign the variable num, num = 1; After this line of code is executed, num is 1. I'm not going to go through the analysis here, but you can easily see what the result is.

If you are a little familiar with C#, as long as you look at the code carefully, you will certainly not make mistakes, but if you test people, I believe that many people will make mistakes. I think this problem is too simple to be a technical problem, so I put it in the non-technical section. The reason for all this nonsense is to show that you need to be careful about even the most familiar and basic things when you're programming. Of course, if one of my people writes code like this, I'm sure I'll kick it when it's reviewed. The fact that you don't assign values to static value type variables directly, and you assign them by method, is not very common or a little bit bt, but I still admire the person who created the problem.

Hopefully this article has been helpful in your C# programming.


Related articles: