Initializing a Static Field / Property

If I have a type like this:

public struct Effect
{
    public int Value { get; set; }

    public static int MinValue = Int32.MinValue;
}

Will MinValueonly ONCE be initialized, just like executing a static constructor? Or should I initialize MinValueinside the static constructor?

Will it be different for classes?

+3
source share
3 answers

It will be executed exactly once, but not quite as if it were in a static constructor. The rules about when type initializers run are different when a type has a static constructor . Note that the observed behavior has changed slightly in .NET 4.0 . Usually you do not need to worry about this.

, .

, MinValue - ? .

+3

, , . ().

+4

Yes, it will be initialized only once per process (application domain).

+4
source

Source: https://habr.com/ru/post/1786516/


All Articles