
Untitled
By: a guest on
Jun 16th, 2012 | syntax:
None | size: 1.12 KB | hits: 14 | expires: Never
How to convert bool? to bool in C#?
bool newBool = (x.HasValue) ? x.Value : false;
bool? nullableBool = null;
bool actualBool = nullableBool.GetValueOrDefault();
if (bn.HasValue)
{
b = bn.Value
}
bool b1;
bool? b2 = ???;
if (b2.HasValue)
b1 = b2.Value;
bool b3 = (b2 == true); // b2 is true, not false or null
bool? x = ...;
if (x ?? true) {
}
bool converted = (bool)x;
bool? b = ...;
if (b == true) { Debug.WriteLine("true"; }
if (b == false) { Debug.WriteLine("false"; }
if (b != true) { Debug.WriteLine("false or null"; }
if (b != false) { Debug.WriteLine("true or null"; }
bool? b = ...;
if (b == null) { Debug.WriteLine("null"; }
if (b != null) { Debug.WriteLine("true or false"; }
if (b.HasValue) { Debug.WriteLine("true or false"; }
//HasValue and != null will ALWAYS return the same value, so use whatever you like.
bool? b = ...;
bool b2 = b ?? true; // null becomes true
b2 = b ?? false; // null becomes false
bool? b = ...;
if(b == null)
throw new ArgumentNullException();
else
SomeFunc(b.Value);
bool? a = null;
bool b = Convert.toBoolean(a);