Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default boolean value in C#?

Tags:

c#

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 ???

like image 211
JohnJohnGa Avatar asked Jul 28 '11 07:07

JohnJohnGa


People also ask

Does boolean have a default value?

The default value of any Object , such as Boolean , is null . The default value for a boolean is false.

What is boolean in C?

A boolean is a data type in the C Standard Library which can store true or false . Every non-zero value corresponds to true while 0 corresponds to false . The boolean works as it does in C++. However, if you don't include the header file​ stdbool. h , the program will not compile.

What is the default value of boolean property?

Use the create() or update() methods to specify the value of the new property as its default value, that is, false for boolean or 0 for integer. The property value should get updated successfully.

What is default boolean and?

AND: AND is the default Boolean operator, and using it will narrow your search results by telling the search engine to return results that have BOTH/ALL search terms present.


2 Answers

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

like image 77
Destructor Avatar answered Oct 11 '22 16:10

Destructor


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.
like image 28
Hasan Fahim Avatar answered Oct 11 '22 14:10

Hasan Fahim