default value for a static property

Blitzz picture Blitzz · Apr 8, 2010 · Viewed 37k times · Source

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

Answer

Corey Sunwold picture Corey Sunwold · Apr 8, 2010

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.