bool

Type Range Default Value
bool true or false false

The bool type represents Boolean values.

Auto-boxing

All primitive types have a corresponding wrapper class. The corresponding wrapper class for the bool type is System.Boolean.

Auto-boxing occurs when a value of a primitive type is assigned or passed as an argument to a variable or function that expects a value of its corresponding wrapper class. For example, the Boolean value true would fit within the range of a bool and can be assigned to a variable of type System.Boolean via auto-boxing:

1
System.Boolean value = true; // Auto-boxing

Without auto-boxing, the equivalent code would be:

1
System.Boolean value = new System.Boolean(true);

The reverse also occurs. Unboxing is when the wrapper class is automatically converted to its corresponding primitive type. Thus, an instance of System.Boolean can be unboxed to the bool primitive type.

1
bool value = new System.Boolean(true); // Unboxing

Conversions for Internal Types

For conversions to and from the bool type, consult the Conversions Table.

Conversions for external Type

When converting from external:

  • If the external value is a zero numeric value (including +0, -0, 0.0, and so on), it will be automatically converted to false.

  • If the external value is a non-zero numeric value, it will be automatically converted to true.

  • If the external value is NaN, it will be automatically converted to false.

  • If the external value is positive or negative Infinity, it will be automatically converted to true.

  • If the external value is a empty string value, it will be automatically converted to false.

  • If the external value is a non-empty string value, it will be automatically converted to true.

  • If the external value is the Boolean true value, it will be automatically converted to true.

  • If the external value is the Boolean false value, it will be automatically converted to false.

  • If the external value is null or undefined, it will be automatically converted to false.

  • If the external value is an array, it will be automatically converted to true.

  • If the external value is an object, it will be automatically converted to true.

  • If the external value is an function, it will be automatically converted to true.

When converting to external, the value will be converted to a JavaScript boolean value.

See Also

Share

HTML | BBCode | Direct Link