Analysis of C Static Class Static Constructor and Static Variable

  • 2021-10-13 08:24:55
  • OfStack

Static variable

Static variable is located on the stack, which is a global variable and has been generated at compile time.


public class Cow
{
public static int count;
private int id;
public Cow()
{
id = ++count;
}
}

The client creates two instances of Cow and prints the static variable count.


static void Main(string[] args)
{
Console.WriteLine(Cow.count);
Cow cow1 = new Cow();
Cow cow2 = new Cow();
Console.WriteLine(Cow.count);
} 

Results:

0
2

0 Before creating an Cow instance, the global already has the static variable count
0 If decorated with private before static, static fields cannot be accessed through "class name. static field name", but global static fields always exist

The performance on heap and stack is as follows:

Static constructor

Add a static constructor to the Cow class.


public class Cow
{
public static int count;
private int id;
public Cow()
{
id = ++count;
}
static Cow()
{
count = new Random().Next(100);
}
}

In both constructors and static constructors, static fields of Cow are assigned values. Now we want to know when the static constructor fires. Is it triggered when an instance is created with a constructor? Will it be triggered when setting the field or property value of Cow? On the client side, you can see when the static constructor is triggered by printing the value of the static field count.


static void Main(string[] args) { Cow cow1 = new Cow(); Console.WriteLine(" Create the 1 A Cow After instance count Is: "+ Cow.count); Cow cow2 = new Cow(); Console.WriteLine(" Create the 2 A Cow After instance count Is: " + Cow.count); }

The 0 static constructor is triggered when the first instance of Cow is created

0 When the second Cow instance is created, the static constructor is not triggered, but the instance is created through the constructor

0 static constructor only executes once

Therefore, can we conclude that the static constructor was triggered when the first instance was created?

Looking at the side of the ridge as a peak, think about this problem from a different angle. Will static constructors be triggered when assigning values to fields of a class?

Modify the Cow class to read:


public class Cow
{
public static int count;
private int id;
public static int whatever;
public Cow()
{
id = ++count;
}
static Cow()
{
count = new Random().Next(100);
whatever = count + 10;
Console.WriteLine(" After the static constructor is triggered, count Is: " + Cow.count);
Console.WriteLine(" After the static constructor is triggered, whatever Is: " + Cow.whatever);
}
}

The client is modified to:


static void Main(string[] args)
{
Cow.count = 100;
Cow cow1 = new Cow();
Console.WriteLine(" Create the 1 A Cow After instance count Is: "+ Cow.count);
Cow cow2 = new Cow();
Console.WriteLine(" Create the 2 A Cow After instance count Is: " + Cow.count); 
} 

0 Before assigning a value to the field of Cow, the static constructor is triggered

0 then creates an Cow instance, and the static constructor is not triggered again

0 static constructor only executes once

At this point, with regard to the timing of the static constructor being triggered, we can conclude that the static constructor is triggered before all these actions, whether it is creating an instance through the constructor or assigning values to the fields or properties of the class.

Static class

First, create a class, including static members and non-static members.


public class Logger
{ private static int logNumber = 0;
static public void InitializeLogging()
{
Console.WriteLine(" Log initialization ");
}
static public void CloseLog()
{
Console.WriteLine(" Log off ");
}
static public void LogMsg(string msg)
{
Console.WriteLine(" The log number is: " + logNumber + ":" + msg);
}
public void DoSth()
{
Console.WriteLine(" I am not a static method ~~");
}
}

On the client side, you can call methods either through "class name. Static method name" or through an instance of the class.


static void Main(string[] args)
{
Logger.InitializeLogging();
Logger.LogMsg(" The log was recorded ~~");
Logger.CloseLog();
Logger logger = new Logger();
logger.DoSth();
}

If you set a class to be static, it means that all the 1 cuts of this class exist on the stack, so there can be no instance methods in this class, and no instance of this class can be created.

Modify the Logger class to remove the instance method.


public static class Logger
{
private static int logNumber = 0;
static public void InitializeLogging()
{
Console.WriteLine(" Log initialization ");
}
static public void CloseLog()
{
Console.WriteLine(" Log off ");
}
static public void LogMsg(string msg)
{
Console.WriteLine(" The log number is: " + logNumber + ":" + msg);
}
}

On the client side, you can't create an instance of Logger, and you can only call methods through "class name. Static method name".


static void Main(string[] args)
{
Logger.InitializeLogging();
Logger.LogMsg(" The log was recorded ~~");
Logger.CloseLog();
} 

Summary:

0 static variables are global and on the stack

The static constructor is only triggered once, and the static constructor is triggered before these actions, whether it is to create an instance by the constructor or to assign values to a field or property of a class

0 Static class cannot have instance members

The above content is this site to introduce you to C # static class, static constructor, static variables all described, I hope to help you!


Related articles: