87

A boolean (bool) can't be null. And:

bool foo; if(foo){} // Use of unassigned local variable 'foo'

Why the default value is not false? So what is the value if it is not null? What is the reason?

Edit 1 - The default value is indeed false - but all variable should be initialized, why? this is for another question ;)

Edit 2 - with Resharper : private bool foo = false; // Initializing field by default value is redundant ???

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
JohnJohnGa
  • 15,446
  • 19
  • 62
  • 87
  • 1
    You might find http://blogs.msdn.com/b/ericlippert/archive/2009/10/12/absence-of-evidence-is-not-evidence-of-absence.aspx interesting – AakashM Jul 28 '11 at 07:54

6 Answers6

110

http://msdn.microsoft.com/en-us/library/83fhsxwc.aspx

Remember that using uninitialized variables in C# is not allowed.

With

bool foo = new bool();

foo will have the default value.

Boolean default is false

Destructor
  • 1,641
  • 1
  • 13
  • 13
  • 1
    Sorry, added: Remember that using uninitialized variables in C# is not allowed. As seen in the linked article – Destructor Jul 28 '11 at 07:54
  • 1
    Thanks! and so how you can explain my "Edit 2" :) – JohnJohnGa Jul 28 '11 at 08:01
  • 1
    the default value will be set if the default constructor is called. You just initialized a new variable with a value. "int i" will throw the same error -Use of unassigned local variable-. Set the value at initializing or call the default constructor. so you will not have any problems :) – Destructor Jul 28 '11 at 08:07
  • 1
    @Destructor I think your comment is slightly confusing. The reason resharper is suggesting that OP does not have to initialize the boolean field is because fields in a class are automatically assigned their default value, which local variables are not. See the [answer from Hasan](https://stackoverflow.com/a/6855795/681538) below. – Alex Jul 09 '20 at 09:31
30

Basically local variables aren't automatically initialized. Hence using them without initializing would result in an exception.

Only the following variables are automatically initialized to their default values:

  • Static variables
  • Instance variables of class and struct instances
  • Array elements

The default values are as follows (assigned in default constructor of a class):

  • The default value of a variable of reference type is null.
  • For integer types, the default value is 0
  • For char, the default value is `\u0000'
  • For float, the default value is 0.0f
  • For double, the default value is 0.0d
  • For decimal, the default value is 0.0m
  • For bool, the default value is false
  • For an enum type, the default value is 0
  • For a struct type, the default value is obtained by setting all value type fields to their default values

As far as later parts of your question are conerned:

  • The reason why all variables which are not automatically initialized to default values should be initialized is a restriction imposed by compiler.
  • private bool foo = false; This is indeed redundant since this is an instance variable of a class. Hence this would be initialized to false in the default constructor. Hence no need to set this to false yourself.
Hasan Fahim
  • 3,875
  • 1
  • 30
  • 51
  • 4
    For completeness, the [default value table at MSDN](http://msdn.microsoft.com/en-us/library/vstudio/83fhsxwc.aspx) says that default values for struct types also includes setting "all reference-type fields to null." – Carl Walsh Oct 22 '13 at 20:59
10

The default value is indeed false.

However you can't use a local variable is it's not been assigned first.

You can use the default keyword to verify:

bool foo = default(bool);
if (!foo) { Console.WriteLine("Default is false"); }
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
4

The default value for bool is false. See this table for a great reference on default values. The only reason it would not be false when you check it is if you initialize/set it to true.

Corey Larson
  • 1,577
  • 18
  • 39
0

Try this (using default keyword)

 bool foo = default(bool); if (foo) { } 
TalentTuner
  • 17,262
  • 5
  • 38
  • 63
0

It can be treated as defensive programming approach from the compiler - the variables must be assigned before it can be used.

Ajay
  • 18,086
  • 12
  • 59
  • 105