Change The Default Value of Boolean

eMi picture eMi · Nov 9, 2012 · Viewed 39.2k times · Source

I'm writing an application, where I have quite a lot Properties of Type Boolean defined:

    private bool kajmak = true;
    public bool Kajmak
    {
        get { return kajmak ; }
        set { kajmak = value; FirePropertyChanged(() => Kajmak); }
    }

As you see, I set kajmak to true at the beginning..-the reason is nonrelevant-. (You might know that the default value of a bool variable is false).

Now, is there a way, to change the default value of a bool to true? So I would write:

private bool kajmak; //kajmak = true

instead of

private bool kajmak = true;

What could I do to achieve this?

Answer

Adam Diament picture Adam Diament · Jan 11, 2016

C Sharp 6.0 has introduced a nice new way to do this:

 public bool YourBool { get; set; } = true;

This is equivalent to the old way of:

    private bool _yourBool = true;

    public bool YourBool 
    {
        get { return _yourBool; }
        set { _yourBool = value; }
    }

see this article http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx