eMi
eMi

Reputation: 5618

Change The Default Value of Boolean

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?

Upvotes: 29

Views: 70095

Answers (7)

Alexandre Lima
Alexandre Lima

Reputation: 324

In service:

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

Upvotes: 12

Adam Diament
Adam Diament

Reputation: 4830

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

Upvotes: 82

KGVT
KGVT

Reputation: 98

In the process of trying to do something similar, a colleague enlightened me to the bool? type. It can be true, false, or null, and does not object to being on the left side of such a comparator. This does not answer your question of how to default bool to true, but does solve your conceptual problem of wanting your variables to be definable as true by default.

I only post because this was the top result when I searched, and this information was helpful to me. Hopefully it will be to others who find this page.

Upvotes: 0

Damian
Damian

Reputation: 107

You may create a class myBool that defaults to false and an implicit conversion from bool to your class.

Upvotes: -2

Davio
Davio

Reputation: 4737

Because booleans are false by default, I use positive forms in my names, like IsInitialized, HasSomething etc. which I want to be false by default until I explicitly set them.

If you find you need something to be true by default, maybe you need to rename your variable so it makes more sense when the default is false.

Upvotes: 27

Justin Niessner
Justin Niessner

Reputation: 245399

No. There's no way to change the default value assigned by .NET. Your best bet is to either assign the appropriate default in the private member:

private book kajmak = false;

Or use the Constructor like you're supposed to and assign the class defaults there:

public class SomeClass
{
    public SomeClass()
    {
        Kajmak = false;
    }

    public book Kajmak { get; set; }
}

Upvotes: 4

looper
looper

Reputation: 1979

No, there's no possibility to change the default value. If you could change the default-value, it wouldn't be the default anymore ;).

But to set the default-value to null, you could use this:

bool? kajmak;

But that's not what you want...

Upvotes: 0

Related Questions