Search code examples
c#staticpropertiesdefault

default value for a static property


I like c#, but why can I do :

public static bool Initialized { private set; get; }

or this :

public static bool Initialized = false;

but not a mix of both in one line ?

I just need to set access level to my variable (private set), and I need it set at false on startup. I wouldn't like to make that boring private _Initialized variable, which would be returned by the getter of the public Initialized var. I like my code to be beautiful. (NB: my variable is static, it can't be initialized in the constructor).

Thanks


Solution

  • You could use a static constructor

    static MyClass()
    {
        Initialized = false;
    }
    

    However, as has been mentioned by others the default value of a bool will be false.