ChatGPT解决这个技术问题 Extra ChatGPT

What is the use of static constructors?

Please explain to me the use of static constructor. Why and when would we create a static constructor and is it possible to overload one?

It should be noted that there are static methods of construction (for an example, look up the Singleton Design Pattern) which are used to hide the actual constructors used to instanciate the class. This gives the author more control about how their class is used.

M
Marc Gravell

No you can't overload it; a static constructor is useful for initializing any static fields associated with a type (or any other per-type operations) - useful in particular for reading required configuration data into readonly fields, etc.

It is run automatically by the runtime the first time it is needed (the exact rules there are complicated (see "beforefieldinit"), and changed subtly between CLR2 and CLR4). Unless you abuse reflection, it is guaranteed to run at most once (even if two threads arrive at the same time).


thanks for reply. can you please provide me more details about your sentence "Unless you abuse reflection, it is guaranteed to run at most once".. what can do with reflection regarding static constructor..
@Rajesh - find the method and call Invoke 20 times.
Hi @MarcGravell, please clarify what's the difference between CLR2 and CLR4 static constructors' behavior. And also, does it mean that static constructors are thread safe?
@Johnny_D pretty sure there are conditions where they can be deferred later in CLR4 - the exact scenario would need some digging. Re thread-safe: in sane scenarios, yes - basically. If you abuse them with reflection: not so much
For anyone interested how static constructor initialization changed from CLR 2 to CLR 4, Jon Skeet's blog post (codeblog.jonskeet.uk/2010/01/26/…) sums it up nicely: CLR 2 uses eager initialization, while CLR 4 uses lazy initialization.
C
Community

From Static Constructors (C# Programming Guide):

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced. Static constructors have the following properties: A static constructor does not take access modifiers or have parameters. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. A static constructor cannot be called directly. The user has no control on when the static constructor is executed in the program. A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file. Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.


M
Mark Meuer

Static constructors are also very useful when you have static fields that rely upon each other such that the order of initialization is important. If you run your code through a formatter/beautifier that changes the order of the fields then you may find yourself with null values where you didn't expect them.

Example: Suppose we had this class:

class ScopeMonitor
{
    static string urlFragment = "foo/bar";
    static string firstPart= "http://www.example.com/";
    static string fullUrl= firstPart + urlFragment;
}

When you access fullUr, it will be "http://www.example.com/foo/bar".

Months later you're cleaning up your code and alphabetize the fields (let's say they're part of a much larger list, so you don't notice the problem). You have:

class ScopeMonitor
{
    static string firstPart= "http://www.example.com/";
    static string fullUrl= firstPart + urlFragment;
    static string urlFragment = "foo/bar";
}

Your fullUrl value is now just "http://www.example.com/" since urlFragment hadn't been initialized at the time fullUrl was being set. Not good. So, you add a static constructor to take care of the initialization:

class ScopeMonitor
{
    static string firstPart= "http://www.example.com/";
    static string fullUrl;
    static string urlFragment = "foo/bar";

    static ScopeMonitor()
    {
        fullUrl= firstPart + urlFragment;

    }
}

Now, no matter what order you have the fields, the initialization will always be correct.


If you are using ReSharper then I believe it will warn you in this situation when attempting to set fullUrl before urlFragment has been initialised. Good example nonetheless.
Do we know that the static constructor will execute after the field intialization every time? I guess not because Andrew's answer above says "A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.".
@NoChance Are you asking if the static constructor can be confident the fields that are initialized directly (in the example above firstPart and urlFragment) will have been initialized before that static constructor is called? The answer is yes, as long as the fields are listed before the static constructor. From MS docs: If static field variable initializers are present in the class of the static constructor, they will be executed in the textual order in which they appear in the class declaration immediately prior to the execution of the static constructor.
@NoChance Re-reading my comment, I believe I misspoke when I said the fields with initializers need to precede the static constructor. They will be initialized before the static constructor is executed regardless of where they are in the file.
r
ravithejag

1.It can only access the static member(s) of the class.

Reason : Non static member is specific to the object instance. If static constructor are allowed to work on non static members it will reflect the changes in all the object instance, which is impractical.

2.There should be no parameter(s) in static constructor.

Reason: Since, It is going to be called by CLR, nobody can pass the parameter to it. 3.Only one static constructor is allowed.

Reason: Overloading needs the two methods to be different in terms of method/constructor definition which is not possible in static constructor.

4.There should be no access modifier to it.

Reason: Again the reason is same call to static constructor is made by CLR and not by the object, no need to have access modifier to it


D
Dot Freelancer

you can use static constructor to initializes static fields. It runs at an indeterminate time before those fields are used. Microsoft's documentation and many developers warn that static constructors on a type impose a substantial overhead. It is best to avoid static constructors for maximum performance. update: you can't use more than one static constructor in the same class, however you can use other instance constructors with (maximum) one static constructor.


It may be exceptionally obvious, but I think this answer hits a key point only implicit in, eg, marc's answer -- Static constructors are not in lieu of instance constructors. They are called once, before the first instance is created, to set up static properties, etc. Instance constructors keep on working like they've always operated, setting up stuff particular to that instance. static constructor:static class concerns::instance constructor:instance level concerns Makes some sense. ;^)
K
Kenny Evitt

Why and when would we create a static constructor ...?

One specific reason to use a static constructor is to create a 'super enum' class. Here's a (simple, contrived) example:

public class Animals
{
    private readonly string _description;
    private readonly string _speciesBinomialName;

    public string Description { get { return _description; } }
    public string SpeciesBinomialName { get { return _speciesBinomialName; } }

    private Animals(string description, string speciesBinomialName)
    {
        _description = description;
        _speciesBinomialName = speciesBinomialName;
    }

    private static readonly Animals _dog;
    private static readonly Animals _cat;
    private static readonly Animals _boaConstrictor;

    public static Animals Dog { get { return _dog; } }
    public static Animals Cat { get { return _cat; } }
    public static Animals BoaConstrictor { get { return _boaConstrictor; } }

    static Animals()
    {
        _dog = new Animals("Man's best friend", "Canis familiaris");
        _cat = new Animals("Small, typically furry, killer", "Felis catus");
        _boaConstrictor = new Animals("Large, heavy-bodied snake", "Boa constrictor");
    }
}

You'd use it very similarly (in syntactical appearance) to any other enum:

Animals.Dog

The advantage of this over a regular enum is that you can encapsulate related info easily. One disadvantage is that you can't use these values in a switch statement (because it requires constant values).


H
Hasan Fathi

Static constructor called only the first instance of the class created. and used to perform a particular action that needs to be performed only once in the life cycle of the class.


J
Joma

From Microsoft documentation https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors

Static Constructors (C# Programming Guide)

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed only once. It is called automatically before the first instance is created or any static members are referenced.

class SimpleClass
{
    // Static variable that must be initialized at run time.
    static readonly long baseline;

    // Static constructor is called at most one time, before any
    // instance constructor is invoked or member is accessed.
    static SimpleClass()
    {
        baseline = DateTime.Now.Ticks;
    }
}

Remarks

Static constructors have the following properties:

A static constructor doesn't take access modifiers or have parameters.

A class or struct can only have one static constructor.

Static constructors cannot be inherited or overloaded.

A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR). It is invoked automatically.

The user has no control on when the static constructor is executed in the program.

A static constructor is called automatically. It initializes the class before the first instance is created or any static members declared in that class (not its base classes) are referenced. A static constructor runs before an instance constructor. A type's static constructor is called when a static method assigned to an event or a delegate is invoked and not when it is assigned. If static field variable initializers are present in the class of the static constructor, they're executed in the textual order in which they appear in the class declaration. The initializers run immediately prior to the execution of the static constructor.

If you don't provide a static constructor to initialize static fields, all static fields are initialized to their default value as listed in Default values of C# types.

If a static constructor throws an exception, the runtime doesn't invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain. Most commonly, a xref:System.TypeInitializationException exception is thrown when a static constructor is unable to instantiate a type or for an unhandled exception occurring within a static constructor. For static constructors that aren't explicitly defined in source code, troubleshooting may require inspection of the intermediate language (IL) code.

The presence of a static constructor prevents the addition of the xref:System.Reflection.TypeAttributes.BeforeFieldInit type attribute. This limits runtime optimization.

A field declared as static readonly may only be assigned as part of its declaration or in a static constructor. When an explicit static constructor isn't required, initialize static fields at declaration rather than through a static constructor for better runtime optimization.

The runtime calls a static constructor no more than once in a single application domain. That call is made in a locked region based on the specific type of the class. No additional locking mechanisms are needed in the body of a static constructor. To avoid the risk of deadlocks, don't block the current thread in static constructors and initializers. For example, don't wait on tasks, threads, wait handles or events, don't acquire locks, and don't execute blocking parallel operations such as parallel loops, Parallel.Invoke and Parallel LINQ queries.

[!Note] Though not directly accessible, the presence of an explicit static constructor should be documented to assist with troubleshooting initialization exceptions.

Usage

A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

Static constructors are also a convenient place to enforce run-time checks on the type parameter that cannot be checked at compile time via type-parameter constraints.