Reputation: 1465
I want to have a default value for a boolean crossover
of false
How can I initialize it?
public class DecisionBar
{
public DateTime bartime
{ get; set; }
public string frequency
{ get; set; }
public bool HH7
{get;set;}
public bool crossover
{get;set;}
public double mfe
{get;set;}
public double mae
{get;set;}
public double entryPointLong
{get;set;}
public double entryPointShort
{get;set;}
}
Upvotes: 1
Views: 281
Reputation: 10357
from MSDN:
The default value of the bool
is false
; The default value of a bool?
variable is null
. Default constructors are invoked by using the new operator, as follows:
var decisionBar = new DecisionBar();
var myBool = decisionBar.crossover; // 'myBool' should be 'false'
The preceding statement has the same effect as the following statement:
var decisionBar = new DecisionBar();
decisionBar.crossover = false;
See this post: How do you give a C# Auto-Property a default value?
Upvotes: 0
Reputation: 2226
The default value of a boolean is false
. I'm not sure what you mean here.
If you want to set it explicitly. Put this in the class.
private bool _crossover = false;
public bool crossover
{
get
{
return _crossover;
}
set
{
_crossover = value;
}
}
Upvotes: 0
Reputation: 13460
private bool _crossover = false;//or true
public bool Crossover {get {return _crossover;}set{_crossover =value;}}
Upvotes: 0
Reputation: 2447
Apart from the fact that the default value is false, there are two options. Obviously they are redundant, but if you wanted the default value to true you could use either method.
Either don't use an auto-implemented property, instead using a backing property;
private bool _crossover = false;
public bool crossover
{
get { return _crossover; }
set { _crossover = value; }
}
or in the constructor;
public DecisionBar()
{
crossover = false;
}
Upvotes: 3
Reputation: 39095
The default value of any bool
is false
so you really don't need to do anything in this case.
If, however, you wanted true
to be the default value, you could have an explicit backing private field of crossover
and initialize that to be true
:
private bool _co = true;
public bool crossover
{
get { return _co; }
set { _co = value; }
}
Upvotes: 1