.NET offers the programmer many strongly typed data types. One of those is the System.Boolean
structure which represents a Boolean value. System.Boolean
, though programmers normally use the bool
alias when declaring Boolean variables, have a value of either true
or false
. These two logical values permit the Boolean data type to represent anything that needs two states, such as, a light switch or indicating whether the player is alive or dead in a video game, and there are many other uses.
System.Boolean
Definition[SerializableAttribute]
[ComVisibleAttribute(true)]
public struct Boolean : IComparable, IConvertible, IComparable<bool>, IEquatable<bool>
The declaration for System.Boolean
resides in the System
namespace, so when you need to make use of it directly you have to reference the System
namespace either directly, or by including it at the top of your module, which in C# is done via the using
compiler directive, that is:
using System;
which almost all classes in C# actually do.
A System.Boolean
variable will take up precisely one byte of memory. Because bool
and System.Boolean
are synonymous, you can use the bool
keyword almost anywhere you would use System.Boolean
, and assign to a bool
variable using the true
and false
literals of C#. These two reserved keywords, true
and false
, are the only valid Boolean values that can be assigned to the System.Boolean
data type.
You can invert a Boolean test by use of the logical not operator, i.e. !
(exclamation point), so that:
bool result = true;
result = !result;
would actually mean that result is equal to Boolean false
.
We could of course re-write the above two statements as:
bool result = !true;
Which would assign Boolean false
to the result
variable, but that makes little actual sense to do at the point of declaration when assigning the true
literal.
All members of the System.Boolean structure are considered to be thread safe so you can call them with impunity from your multi-threaded code without having to worry about synchronization issues.
The System.Boolean type exposes the following methods and fields:
Name | Description | |
---|---|---|
CompareTo(Boolean) | Compare to another Boolean object and return an integer indicating comparison relationship. | |
CompareTo(Object) | Compare to another object and return an integer indicating the comparison relationship. | |
Equals(Boolean) | Return a value indicating if this instance is equal to the specified Boolean object. | |
Equals(Object) | Return a value indicating if this instance is equal to the specified object. | |
Finalize | Attempt to free allocated resources and perform any cleanup necessary before being garbage collected. | |
GetHashCode | Return the hash code of this object. | |
GetType | Return the Type of the instance. | |
GetTypeCode | Return the TypeCode for the value type Boolean. | |
MemberwiseClone | Perform a shallow copy of this instance. | |
Parse | Attempt to convert a string to its Boolean equivalent. Throws an exception if not possible. | |
ToString() | Convert this instance to its equivalent string representation, i.e. "True" or "False." | |
ToString(IFormatProvider) | Convert this instance to its equivalent string representation, i.e. "True" or "False." | |
TryParse | Attempt to convert a string to its Boolean equivalent. Return value indicates success or failure. |
Name | Description | |
---|---|---|
FalseString | The Boolean value false as a string. Read-only. | |
TrueString | The Boolean value true as a string. Read-only. |
Name | Description | |
---|---|---|
IConvertible.ToBoolean | Converts the value to an equivalent Boolean value. | |
IConvertible.ToByte | Converts the value to an equivalent 8-bit unsigned integer. | |
IConvertible.ToChar | Attempts to convert to this type will throw an InvalidCastException. | |
IConvertible.ToDateTime | Attempts to convert to this type will throw an InvalidCastException. | |
IConvertible.ToDecimal | Converts the value to an equivalent Decimal number. | |
IConvertible.ToDouble | Converts the value to an equivalent double-precision floating-point number. | |
IConvertible.ToInt16 | Converts the value to an equivalent 16-bit signed integer. | |
IConvertible.ToInt32 | Converts the value to an equivalent 32-bit signed integer. | |
IConvertible.ToInt64 | Converts the value to an equivalent 64-bit signed integer. | |
IConvertible.ToSByte | Converts the value to an equivalent 8-bit signed integer. | |
IConvertible.ToSingle | Converts the value to an equivalent single precision floating-point number. | |
IConvertible.ToType | Converts the value to an Object of the specified Type with the equivalent value. | |
IConvertible.ToUInt16 | Converts the value to an equivalent 16-bit unsigned integer. | |
IConvertible.ToUInt32 | Converts the value to an equivalent 32-bit unsigned integer. | |
IConvertible.ToUInt64 | Converts the value to an equivalent 64-bit unsigned integer. |