BCL mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0 4.0.0.0 This type is safe for multithreaded operations. System.ValueType System.IComparable System.IComparable<System.Boolean> System.IConvertible System.IEquatable<System.Boolean> System.Runtime.InteropServices.ComVisible(true)

A instance can have either of two values: true, or false.

The structure provides methods that support the following tasks:

Converting Boolean values to strings:

Parsing strings to convert them to Boolean values: and

Comparing values: and

The following sections explain these tasks and other usage details:

Formatting Boolean values
Converting to and from Boolean values
Parsing Boolean values
Comparing Boolean values
Working with Booleans as binary values
Performing operations with Boolean values

## Formatting Boolean values

The string representation of a is either "True" for a true value or "False" for a false value. The string representation of a value is defined by the read-only and fields.

You use the method to convert Boolean values to strings. The Boolean structure includes two overloads: the parameterless method and the method, which includes a parameter that controls formatting. However, because this parameter is ignored, the two overloads produce identical strings. The method does not support culture-sensitive formatting.

The following example illustrates formatting with the method. Note that the example uses the composite formatting feature, so the method is called implicitly.

code reference: System.Boolean.Structure#3

Because the structure can have only two values, it is easy to add custom formatting. For simple custom formatting in which other string literals are substituted for "True" and "False", you can use any conditional evaluation feature supported by your language, such as the conditional operator in C# or the If operator in Visual Basic. The following example uses this technique to format values as "Yes" and "No" rather than "True" and "False".

code reference: System.Boolean.Structure#4

For more complex custom formatting operations, including culture-sensitive formatting, you can call the method and provide an implementation. The following example implements the and interfaces to provide culture-sensitive Boolean strings for the English (United States), French (France), and Russian (Russia) cultures.

code reference: System.Boolean.Structure#5

Optionally, you can use resource files to define culture-specific Boolean strings.

## Converting to and from Boolean values

The structure implements the interface. As a result, you can use the class to perform conversions between a value and any other primitive type in the .NET Framework, or you can call the structure's explicit implementations. However, conversions between a and the following types are not supported, so the corresponding conversion methods throw an exception:

Conversion between and (the and methods)

Conversion between and (the and methods)

All conversions from integral or floating-point numbers to Boolean values convert non-zero values to true and zero values to false. The following example illustrates this by calling selected overloads of the class.

code reference: System.Boolean.Structure#6

When converting from floating-point values to Boolean values, the conversion methods perform an exact comparison with zero. If the floating-point value has lost precision, the result can be unexpected. This is illustrated in the following example, in which a variable whose value should be zero is converted to a Boolean value. As the example shows, the result is true because repeated additions of 0.2 have resulted in a loss of precision.

When converting from Boolean to numeric values, the conversion methods of the class convert true to 1 and false to 0. However, Visual Basic conversion functions convert true to either 255 (for conversions to values) or -1 (for all other numeric conversions). The following example converts true to numeric values by using a method, and, in the case of the Visual Basic example, by using the Visual Basic language's own conversion operator.

code reference: System.Boolean.Structure#8

For conversions from to string values, see the Formatting Boolean Values section. For conversions from strings to values, see the Parsing Boolean Values section.

## Parsing Boolean values

The structure includes two static parsing methods, and , that convert a string to a Boolean value. The string representation of a Boolean value is defined by the case-insensitive equivalents of the values of the and fields, which are "True" and "False", respectively. In other words, the only strings that parse successfully are "True", "False", "true", "false", or some mixed-case equivalent. You cannot successfully parse numeric strings such as "0" or "1". Leading or trailing white-space characters are not considered when performing the string comparison.

The following example uses the and methods to parse a number of strings. Note that only the case-insensitive equivalents of "True" and "False" can be successfully parsed.

code reference: System.Boolean.Structure#2

If you are programming in Visual Basic, you can use the CBool function to convert the string representation of a number to a Boolean value. "0" is converted to false, and the string representation of any non-zero value is converted to true. If you are not programming in Visual Basic, you must convert your numeric string to a number before converting it to a Boolean. The following example illustrates this by converting an array of integers to Boolean values.

code reference: System.Boolean.Structure#9

## Comparing Boolean values

Because Boolean values are either true or false, there is little reason to explicitly call the method, which indicates whether an instance is greater than, less than, or equal to a specified value. Typically, to compare two Boolean variables, you call the method or use your language's equality operator.

However, when you want to compare a Boolean variable with the literal Boolean value true or false, it is not necessary to do an explicit comparison, because the result of evaluating a Boolean value is that Boolean value. For example, the expressions

code reference: System.Boolean.Structure#12

and

code reference: System.Boolean.Structure#13

are equivalent, but the second is more compact. However, both techniques offer comparable performance.

## Working with Booleans as binary values

A Boolean value occupies one byte of memory. The byte's low-order bit is used to represent its value. A value of 1 represents true; a value of 0 represents false.

You can use the structure to work with sets of Boolean values.

You can convert a Boolean value to its binary representation by calling the method. The method returns a byte array with a single element. To restore a Boolean value from its binary representation, you can call the method.

The following example calls the method to convert a Boolean value to its binary representation and displays the individual bits of the value, and then calls the method to restore the value from its binary representation.

code reference: System.Boolean.Structure#1

## Performing operations with Boolean values

This section illustrates how Boolean values are used in apps. The first section discusses its use as a flag. The second illustrates its use for arithmetic operations.

## Boolean values as flags

Boolean variables are most commonly used as flags, to signal the presence or absence of some condition. For example, in the method, the final parameter, , is a flag that indicates whether the comparison of two strings is case-insensitive ( is true) or case-sensitive ( is false). The value of the flag can then be evaluated in a conditional statement.

The following example uses a simple console app to illustrate the use of Boolean variables as flags. The app accepts command-line parameters that enable output to be redirected to a specified file (the /f switch), and that enable output to be sent both to a specified file and to the console (the /b switch). The app defines a flag named isRedirected to indicate whether output is to be sent to a file, and a flag named isBoth to indicate that output should be sent to the console.

code reference: System.Boolean.Structure#10

## Booleans and arithmetic operations

A Boolean value is sometimes used to indicate the presence of a condition that triggers a mathematical calculation. For example, a hasShippingCharge variable might serve as a flag to indicate whether to add shipping charges to an invoice amount.

Because an operation with a false value has no effect on the result of an operation, it is not necessary to convert the Boolean to an integral value to use in the mathematical operation. Instead, you can use conditional logic.

The following example computes an amount that consists of a subtotal, a shipping charge, and an optional service charge. The hasServiceCharge variable determines whether the service charge is applied. Instead of converting hasServiceCharge to a numeric value and multiplying it by the amount of the service charge, the example uses conditional logic to add the service charge amount if it is applicable.

code reference: System.Boolean.Structure#13

Represents a Boolean (true or false) value.

- Method
  2.0.0.0
  4.0.0.0
  System.Int32 ``

  This method implements the interface and performs slightly better than the method because it does not have to convert the parameter to an object.

  If has fewer bits (is narrower) than the instance type, some programming languages perform an implicit widening conversion that transforms the value of the parameter into a value with more bits.

  For example, suppose the instance type is and the parameter type is . The Microsoft C# compiler generates instructions to represent the value of the parameter as an object, then generates a method to compare the instance and parameter representation.

  Consult your programming language's documentation to determine whether its compiler performs implicit widening conversions on numeric types.

  Compares this instance to a specified object and returns an integer that indicates their relationship to one another.

  A signed integer that indicates the relative values of this instance and .

  Return Value

  Condition

  Less than zero

  This instance is false and is true.

  Zero

  This instance and are equal (either both are true or both are false).

  Greater than zero

  This instance is true and is false.

  A object to compare to this instance.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Int32 `` is not a and is not a null reference.

  must be null or an instance of Boolean; otherwise, an exception is thrown.

  This method implements the interface.

  Compares this instance to a specified object and returns an integer that indicates their relationship to one another.

  A signed integer that indicates the relative order of this instance and .

  Return Value

  Condition

  Less than zero

  This instance is false and is true.

  Zero

  This instance and are equal (either both are true or both are false).

  Greater than zero

  This instance is true and is false.

  -or-

  is null.

  An object to compare to this instance, or null. 0
- Method
  2.0.0.0
  4.0.0.0
  System.Boolean ``

  This method implements the interface, and performs slightly better than because it does not have to convert the parameter to an object.

  Returns a value indicating whether this instance is equal to a specified object.

  true if has the same value as this instance; otherwise, false.

  A value to compare to this instance.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean `` An object to compare to this instance.

  This method overrides .

  Returns a value indicating whether this instance is equal to a specified object.

  true if is a and has the same value as this instance; otherwise, false.

  An object to compare to this instance. 0
- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.String False

  This field is equal to the string "False".

  Represents the Boolean value false as a string. This field is read-only.

  0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Int32

  The class implements true as the integer, one, and false as the integer, zero. However, a particular programming language might represent true and false with other values.

  Returns the hash code for this instance.

  A hash code for the current .

  0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.TypeCode To be added.

  Returns the for value type .

  The enumerated constant, .

- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean `` is a null reference. is not equivalent to either or .

  The following example demonstrates the method.

  `using System; public class BoolParse { public static void Main() { Boolean b = Boolean.Parse(" true "); Console.WriteLine("\" true \" parses to \"{0}\".", b); } }`

  The output is

  " true " parses to "True".

  The parameter, optionally preceded or trailed by white space, must contain a string that is equal to the value of either the or field; otherwise, a is thrown. The comparison is ordinal and case-insensitive.

  You can call the method to parse a string without having to handle the exception that is thrown if the parsing operation fails.

  Converts the specified string representation of a logical value to its equivalent, or throws an exception if the string is not equal to the value of or .

  true if is equal to the value of the field; false if is equal to the value of the field.

  A string containing the value to convert. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface.

  For a description of this member, see .

  true or false.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Byte ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  1 if the value of this instance is true; otherwise, 0.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Char `` To be added.

  This conversion is not supported. Attempting to use this method throws an .

  This conversion is not supported. No value is returned.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.DateTime `` To be added.

  This conversion is not supported. Attempting to use this method throws an .

  This conversion is not supported. No value is returned.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Decimal ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see ..

  1 if this instance is true; otherwise, 0.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Double ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see ..

  1 if this instance is true; otherwise, 0.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Int16 ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  1 if this instance is true; otherwise, 0.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Int32 ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  1 if this instance is true; otherwise, 0.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Int64 ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  1 if this instance is true; otherwise, 0.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.SByte ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  1 if this instance is true; otherwise, 0.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Single ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see ..

  1 if this instance is true; otherwise, 0.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Object `` `` To be added.

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  An object of the specified type, with a value that is equivalent to the value of this Boolean object.

  An implementation that supplies culture-specific information about the format of the returned value.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.UInt16 ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  1 if this instance is true; otherwise, 0.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.UInt32 ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  1 if this instance is true; otherwise, 0.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.UInt64 ``

  This member is an explicit interface member implementation. It can be used only when the instance is cast to an interface. The recommended alternative is to call the method.

  For a description of this member, see .

  1 if this instance is true; otherwise, 0.

  This parameter is ignored.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.String

  This method returns the constants "True" or "False". Note that XML is case-sensitive, and that the XML specification recognizes "true" and "false" as the valid set of Boolean values. If the object returned by the method is to be written to an XML file, its method should be called first to convert it to lowercase.

  Converts the value of this instance to its equivalent string representation (either "True" or "False").

  if the value of this instance is true, or if the value of this instance is false.

  0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.String ``

  The parameter is reserved. It does not participate in the execution of this method. This means that the method, unlike most methods with a parameter, does not reflect culture-specific settings.

  This method returns the constants "True" or "False". Note that XML is case-sensitive, and that the XML specification recognizes "true" and "false" as the valid set of Boolean values. If the object returned by the method is to be written to an XML file, its method should be called first to convert it to lowercase.

  Converts the value of this instance to its equivalent string representation (either "True" or "False").

  if the value of this instance is true, or if the value of this instance is false.

  (Reserved) An object. 0
- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.String True

  This field is equal to the string "True".

  Represents the Boolean value true as a string. This field is read-only.

  0
- Method
  2.0.0.0
  4.0.0.0
  System.Boolean `` `` To be added. To be added. To be added. To be added. To be added.
0