default value for a static property

40,987

Solution 1

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.

Solution 2

Since C# 6:

public static bool Initialized { private set; get; } = false;

Solution 3

You can just do:

public static bool Initialized { private set; get; }

Since bool values are always false by default, there's no need to initialize it.

If you need this to be true by default, or to have more complex logic, you need to do this in a static constructor or use a backing field.

As for "I like my code to be beautiful" - personally, for non-default initialization, I think this is just as "beautiful":

private static bool initialized = true;
public static bool Initialized { get { return initialized; } }

This makes the initialization to a non default very visible, which is not a bad thing.

Solution 4

The two blocks of code you have mentioned are two different things.

The first block is an auto implemented property defination. This is syntactic sugar for a full property defination which looks like this:

private static bool _initialized;
public static bool Initialized
{
    private set
    {
        _initialized = value;
    }
    get
    {
        return _initialized;
    }
}

Your second block of code is a static member definition. If you look at the expansion I have given above, you'll notice that it includes a private static member definition. If you want to provide an initial value you can do it here:

private static bool _initialized = false;
public static bool Initialized
{
    private set
    {
        _initialized = value;
    }
    get
    {
        return _initialized;
    }
}

The inline property definition you are using was designed just to make code a bit shorter in the most common case. If you want to do anything else, you can use the full form of the property code.

Alternatively, you can go down a completely different route and use a static constructor. (See Corey's answer)

Share:
40,987

Related videos on Youtube

Blitzz
Author by

Blitzz

Updated on July 19, 2020

Comments

  • Blitzz
    Blitzz almost 4 years

    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

    • snicker
      snicker about 14 years
      It can be initialized in the constructor if the constructor is static too!
    • Preet Sangha
      Preet Sangha about 14 years
      Won't it be FALSE by default anyway lol? Anyway I appreciate that you're asking the general question.
    • Olivier Jacot-Descombes
      Olivier Jacot-Descombes about 12 years
      Unlike local variables, which are undefined by default, fields (class level variables) and thus also backing variables of properties are always initialized to their default value, which is false for Booleans.
  • snicker
    snicker about 14 years
    Static constructors are the life of the party.
  • snicker
    snicker about 14 years
    I don't think it is "as beautiful", from the perspective of Intellisense, you now have two variables that are accessible with the same name, only with different case, one of which is completely read-only. Also.. (this is purely from the perspective of a devil's advocate) what if the C# spec changes and the default value for bool becomes true? Unlikely, but it is better to be explicit than to assume.
  • snicker
    snicker about 14 years
    "Syntactic sugar" is what makes "beautiful code". I'm sure that the OP knows that this is what is really happening, but has specifically requested a solution that is of comparable "beauty"... for which I think the static constructor is the best path.
  • Reed Copsey
    Reed Copsey about 14 years
    @snicker: The default for bool is part of the C# spec - that won't change. As far as intellisense goes, you'll only see the "Initializer" read only property unless you're inside of a method within the class, in which case, it's pretty obvious which is which by casing...
  • snicker
    snicker about 14 years
    I know, I was making a half-joke about the spec. I still wouldn't want to see "initialized" and "Initialized" in the list when I often will type in lowercase and use tab-completion. I won't know for sure which one I will get (it will be the last one selected using Intellisense)