Boolean values: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: conversely not "=true")
m (C# Removed the remark about C#8 nullable types because it does not apply to bool.)
Line 708: Line 708:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
In C#, there are the reserved keywords <code>true</code> and <code>false</code>. Variables to hold these values are declared as either <code>bool</code> or <code>Boolean</code>. These types are identical, as <code>bool</code> is just shortand for <code>Boolean</code>. The collection type <code>BitArray</code> returns its values as <code>Boolean</code>, packing 8 values into each byte (In contrast, the <code>Boolean</code> type uses the entire byte for one value).
In C#, there are the reserved keywords <code>true</code> and <code>false</code>. Variables to hold these values are declared as either <code>bool</code> or <code>Boolean</code>. These types are identical, as <code>bool</code> is just shorthand for <code>Boolean</code>. The collection type <code>BitArray</code> returns its values as <code>Boolean</code>, packing 8 values into each byte (In contrast, the <code>Boolean</code> type uses the entire byte for one value).


In C# 8.0 nullable type was introduced and when applied to <code>bool</code> supports all values and an additional <code>null</code> value and useful for some applications where the value can be undefined or missing.
There is also the <code>Nullable<T></code> type that represents all values of its underlying value type <code>T</code> and an additional <code>null</code> value. It has a shorthand notation: <code>T?</code> (When <code>T</code> is a reference type, <code>T?</code> means something else. It is not a different type, but just a hint to the compiler.)
So, when applied to <code>bool</code>, we have a <code>bool?</code> type that supports 3 values: <code>true</code>, <code>false</code> and <code>null</code>. This can be useful for some applications where the value can be undefined or missing.


<lang csharp>bool? value = null</lang>
<lang csharp>bool? value = null</lang>