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?
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 .
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 .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With