Simple analysis of C # static class, static constructor, static variable _c# tutorial

Source: Internet
Author: User
Tags instance method static class

static variables

The static variable is on the stack and is a global variable that has been generated at compile time.

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

The client creates 2 cow instances 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 A static variable with count is already in the global before the cow instance is created
0 if you decorate with private before static, you cannot access static fields through the class name. static field name, but the global static field always exists

The performance on the heap and stack, as shown in the following figure:

Static constructors

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 ();
}

In constructors and static constructors, you assign values to cow static fields. Now we want to know when the static constructor is triggered. Is it triggered when you create an instance with a constructor? Will it be triggered when setting the Cow field or property value? At the client, you can see when a 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 first Cow instance after count is:" + cow.count); Cow cow2 = new Cow (); Console.WriteLine ("Create a second Cow instance after count is:" + cow.count); }

0 Static constructors are triggered when the first cow instance is created

0 when the second cow instance is created, the static constructor is not triggered, but the instance is created by the constructor

0 A static constructor executes only once

Thus, can we conclude that a static constructor is triggered when the first instance is created?

Cross as ridge side into the peak, to change the angle of thinking about this problem. Will a static constructor be triggered when assigning a value to a field of a class?

Modify the Cow class to:

public class Cow
{public
static int count;
private int id;
public static int whatever;
Public Cow ()
{
id = ++count;
}
Static Cow ()
{
count = new Random (). Next (m);
Whatever = Count + ten;
Console.WriteLine ("The static constructor is triggered count is:" + cow.count);
Console.WriteLine ("The static constructor is triggered whatever as:" + Cow.whatever);
}

the client is modified to:

static void Main (string[] args)
{
cow.count = m;
Cow Cow1 = new Cow ();
Console.WriteLine ("Create the first Cow instance after count is:" + cow.count);
Cow cow2 = new Cow ();
Console.WriteLine ("Create a second Cow instance after count is:" + cow.count); 

0 A static constructor is triggered before assigning a value to a cow field

0 Then create the cow instance, the static constructor will not be fired again

0 A static constructor executes only once

Here, with regard to the timing of a static constructor being triggered, we can conclude that the static constructor is triggered before all of these actions, whether by creating an instance from a constructor or assigning a value to a class's field or property.

Static class

First, create a class that includes both static 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 ("Log number is:" + Lognumber + ":" + msg);
}
public void Dosth ()
{
Console.WriteLine ("I am not a static method ~");
}

At the client, the method can be invoked either through the class name. static method name, or by an instance of the class.

static void Main (string[] args)
{
logger.initializelogging ();
Logger.logmsg ("The log has been recorded ~ ~");
Logger.closelog ();
Logger Logger = new Logger ();
Logger. Dosth ();
}

If you set a class to a static class, it means that all of this class exists on the stack, so you cannot have an instance method in the class or create an instance of that class.

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 ("Log number is:" + Lognumber + ":" + msg);
}

On the client side, you cannot create an instance of logger, only by calling the method through the class name. static method name.

static void Main (string[] args)
{
logger.initializelogging ();
Logger.logmsg ("The log has been recorded ~ ~");
Logger.closelog ();

Summarize:

0 static variables belong to the global, located on the stack

0 A static constructor is triggered only once, whether by creating an instance from a constructor or assigning a value to a class's fields or properties, which are triggered by a static constructor before these actions

0 cannot have instance members in a static class

The above content is small series to introduce the C # static class, static constructor, static variable of all the narration, hope to help everyone!

Related Article

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.