"Reprint" A few notes on C # static constructors

Source: Internet
Author: User
Tags modifiers

first, the definition

A static constructor is a new feature of C #, which seems to be seldom used. But we need to use it when we want to initialize some static variables. This constructor belongs to the class, not to the instance where it belongs, meaning that the constructor will only be executed once . This is called automatically by. NET before the first instance is created or any static members are referenced.

Class simpleclass{//Static constructorstatic simpleclass ()    {//    }}

There are a few things to keep in mind when using static constructors:
1. Static constructors have neither access modifiers nor parameters. Because it is. NET calls, so modifiers like public and private are meaningless.
2, is when the first class instance is created or any static member is referenced. NET will automatically call the static constructor to initialize the class, which means that we cannot call the static constructor directly, and we cannot control when the static constructor is executed.
3. A class can have only one static constructor.
4. Parameterless constructors can coexist with static constructors. Although the parameter list is the same, one belongs to the class and one belongs to the instance, so there is no conflict.
5, run up to one time only.
6. Static constructors cannot be inherited.
7. If a static constructor is not written, and the class contains a static member with an initial value set, the compiler automatically generates a default static constructor.

8. Thread Safety

With regard to thread safety, it is necessary to note that because a program may run in a multithreaded environment, it is possible that multiple threads are ready to execute a static constructor. The CLR ensures that the process is secure, and the thread that actually calls the static constructor needs to obtain a mutex thread synchronization lock, and if there are multiple threads trying to execute a static constructor of the type, only one thread can get the lock; the thread that obtains the lock completes the initial type initialization operation, and the other threads can wait only when initialization is complete. The waiting thread wakes up and then discovers that the static constructor has been executed and no longer executes.

second, the role

Used to initialize static members. Sometimes we read some values from the configuration file as static variables, like this:

public class Statictester{private static readonly string key = configurationmanager.appsettings["key"];p rivate static ReadOnly String value = configurationmanager.appsettings["Value"];static statictester () {           }}
If you want to read more configuration information and add some logical judgments, you can:
public class statictester{    private static readonly string key;    private static readonly string value;         Static Statictester ()    {        key = configurationmanager.appsettings["key"];        if (string. IsNullOrEmpty (key))        {            throw new Exception ("key variable not configured properly!) ");                            }        Value = configurationmanager.appsettings["value"];        if (string. IsNullOrEmpty (value)        {            throw new Exception ("The value variable is not configured properly!) ");        }    }}

iii. Order of execution

1. At runtime, initialize static variables first.

2. If there is a static constructor, the static constructor is called before the first instance object is created or any static variables are referenced.

3. Other operations.

"Turn from" https://www.cnblogs.com/4littleProgrammer/p/4883012.html

Https://www.cnblogs.com/michaelxu/archive/2007/03/29/693401.html

"Reprint" A few notes on C # static constructors

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.