1

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;}


}
Anish Gupta
  • 2,218
  • 2
  • 23
  • 37
junkone
  • 1,427
  • 1
  • 20
  • 45
  • depends on what you are trying to achieve. You could put it in the constructor and initialise that way. But the object will always have to be instantiated for the value to be set. What are you trying to achieve? – Nicholas King Jul 23 '12 at 10:48
  • Try read about constructors. public DecisionBar() { crossover = false; } – Independent Jul 23 '12 at 10:49
  • Please have a look : **http://msdn.microsoft.com/en-us/library/ace5hbzh%28v=vs.71%29.aspx** – huMpty duMpty Jul 23 '12 at 10:50
  • may be duplicate of http://stackoverflow.com/questions/40730/how-do-you-give-a-c-sharp-auto-property-a-default-value – Ria Jul 23 '12 at 10:53
  • The default value of `crossover` is already `false` – Security Hound Jul 23 '12 at 11:20
  • Is it important to software design, that a bool have false on instantiation? I would set its default explicitly in a constructor, as a clarification to anyone who may see the code. – Independent Jul 23 '12 at 12:57

5 Answers5

3

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;
}
tbddeveloper
  • 2,407
  • 1
  • 23
  • 39
1

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; }
}
Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
0

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;
    }
}
Anish Gupta
  • 2,218
  • 2
  • 23
  • 37
0
private bool _crossover = false;//or true
public bool Crossover {get {return _crossover;}set{_crossover =value;}}
burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
0

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?

Community
  • 1
  • 1
Ria
  • 10,237
  • 3
  • 33
  • 60