167

How do you convert a nullable bool? to bool in C#?

I have tried x.Value or x.HasValue ...

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
KentZhou
  • 24,805
  • 41
  • 134
  • 200

10 Answers10

287

You ultimately have to decide what the null bool will represent. If null should be false, you can do this:

bool newBool = x.HasValue ? x.Value : false;

Or:

bool newBool = x.HasValue && x.Value;

Or:

bool newBool = x ?? false;
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
Ken Pespisa
  • 21,989
  • 3
  • 55
  • 63
128

You can use the null-coalescing operator: x ?? something, where something is a boolean value that you want to use if x is null.

Example:

bool? myBool = null;
bool newBool = myBool ?? false;

newBool will be false.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
94

You can use Nullable{T} GetValueOrDefault() method. This will return false if null.

 bool? nullableBool = null;

 bool actualBool = nullableBool.GetValueOrDefault();
SteveC
  • 15,808
  • 23
  • 102
  • 173
Joel Briggs
  • 1,779
  • 2
  • 14
  • 17
  • 9
    I think this is the best hybrid between conciseness and C# noob-friendliness. Also note that there is an overload where you can specify the default value. – Phil May 20 '11 at 18:10
  • 5
    I like using this method, because it can create 'elegant' if statements `if (nullableBool.GetValueOrDefault())` – Luc Wollants Aug 26 '14 at 07:48
11

If you're going to use the bool? in an if statement, I find the easiest thing to do is to compare against either true or false.

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"; }

Of course, you can also compare against null as well.

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.

If you're going to convert it to a bool to pass on to other parts of the application, then the Null Coalesce operator is what you want.

bool? b = ...;
bool b2 = b ?? true; // null becomes true
b2 = b ?? false; // null becomes false

If you've already checked for null, and you just want the value, then access the Value property.

bool? b = ...;
if(b == null)
    throw new ArgumentNullException();
else
    SomeFunc(b.Value);
David Yaw
  • 27,383
  • 4
  • 60
  • 93
7

The easiest way is to use the null coalescing operator: ??

bool? x = ...;
if (x ?? true) { 

}

The ?? with nullable values works by examining the provided nullable expression. If the nullable expression has a value the it's value will be used else it will use the expression on the right of ??

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
5

This answer is for the use case when you simply want to test the bool? in a condition. It can also be used to get a normal bool. It is an alternative I personnaly find easier to read than the coalescing operator ??.

If you want to test a condition, you can use this

bool? nullableBool = someFunction();
if(nullableBool == true)
{
    //Do stuff
}

The above if will be true only if the bool? is true.

You can also use this to assign a regular bool from a bool?

bool? nullableBool = someFunction();
bool regularBool = nullableBool == true;

witch is the same as

bool? nullableBool = someFunction();
bool regularBool = nullableBool ?? false;
Rémi
  • 3,867
  • 5
  • 28
  • 44
3
bool? a = null;
bool b = Convert.toBoolean(a); 
patrick
  • 16,091
  • 29
  • 100
  • 164
2

Something like:

if (bn.HasValue)
{
  b = bn.Value
}
Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
manojlds
  • 290,304
  • 63
  • 469
  • 417
2

The complete way would be:

bool b1;
bool? b2 = ???;
if (b2.HasValue)
   b1 = b2.Value;

Or you can test for specific values using

bool b3 = (b2 == true); // b2 is true, not false or null
CodeNaked
  • 40,753
  • 6
  • 122
  • 148
0

This is an interesting variation on the theme. At first and second glances you would assume the true branch is taken. Not so!

bool? flag = null;
if (!flag ?? true)
{
    // false branch
}
else
{
    // true branch
}

The way to get what you want is to do this:

if (!(flag ?? true))
{
    // false branch
}
else
{
    // true branch
}
stevieg
  • 652
  • 4
  • 14