Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 983
eMi Avatar asked Nov 09 '12 14:11

eMi


People also ask

How do I change the default boolean value in Java?

There is no default for Boolean . Boolean must be constructed with a boolean or a String . If the object is unintialized, it would point to null . The default value of primitive boolean is false .

What is the default value of boolean?

The default value of Boolean is False . Boolean values are not stored as numbers, and the stored values are not intended to be equivalent to numbers. You should never write code that relies on equivalent numeric values for True and False .

How do you set a boolean to default?

Set a default valueRight-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value. Press CTRL+S to save your changes.


1 Answers

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

like image 169
Adam Diament Avatar answered Oct 06 '22 02:10

Adam Diament