Best way to check for nullable bool in a condition expression (if ...)

C#Coding StyleNullable

C# Problem Overview


I was wondering what was the most clean and understandable syntax for doing condition checks on nullable bools.

Is the following good or bad coding style? Is there a way to express the condition better/more cleanly?

bool? nullableBool = true;
if (nullableBool ?? false) { ... }
else { ... }

especially the if (nullableBool ?? false) part. I don't like the if (x.HasValue && x.Value) style ...

(not sure whether the question has been asked before ... couldn't find something similar with the search)

C# Solutions


Solution 1 - C#

I think a lot of people concentrate on the fact that this value is nullable, and don't think about what they actually want :)

bool? nullableBool = true;
if (nullableBool == true) { ... } // true
else { ... } // false or null

Or if you want more options...

bool? nullableBool = true;
if (nullableBool == true) { ... } // true
else if (nullableBool == false) { ... } // false
else { ... } // null

(nullableBool == true) will never return true if the bool? is null :P

Solution 2 - C#

How about using GetValueOrDefault, which is pretty self-explaining and allows to use whatever default you want:

if (nullableBool.GetValueOrDefault(false)) {
}

Solution 3 - C#

You may not like it, but personally I find

if (x.HasValue && x.Value)

the most readable. It makes it clear you are working with a nullable type and it makes it clear you are first checking whether the nullable type has a value before acting on it conditionally.

If you take your version and replace the variable with x also it reads:

if (x ?? false)

Is that as clear? Is it obvious x is a nullable type? I'll let you decide.

Solution 4 - C#

If you want to treat a null as false, then I would say that the most succinct way to do that is to use the null coalesce operator (??), as you describe:

if (nullableBool ?? false) { ... }

Solution 5 - C#

Another way is to use constant pattern matching:

if (nullableBool is true) {}
if (nullableBool is false) {}
if (nullableBool is null) {}

Unlike the operator ==, when reading the code, this will distinguish the nullable type check from ordinary "code with a smell".

Solution 6 - C#

Just think of bool? as having 3 values, then things get easier:

if (someNullableBool == true)     // only if true
if (someNullableBool == false)    // only if false
if (someNullableBool == null)     // only if null

Solution 7 - C#

Actually I think that (nullableBool ?? false) is a legitimate option especially when you are trying to evaluate a nullable bool in linq.

For example:

array.Select(v => v.nullableBool ?? false)
(from v in array where v.nullableBool ?? false)

Is cleaner in my opinion as opposed to:

array.Select(v => v.nullableBool.HasValue ? v.nullableBool.Value : false)
(from v in array where v.nullableBool.HasValue ? v.nullableBool.Value : false)

Solution 8 - C#

Use extensions.

public static class NullableMixin {
    public static bool IsTrue(this System.Nullable<bool> val) {
        return val == true;
    }
    public static bool IsFalse(this System.Nullable<bool> val) {
        return val == false;
    }
    public static bool IsNull(this System.Nullable<bool> val) {
        return val == null;
    }
    public static bool IsNotNull(this System.Nullable<bool> val) {
        return val.HasValue;
    }
}


Nullable<bool> value = null;
if(value.IsTrue()) {
// do something with it
}
     

Solution 9 - C#

Lets check how the comparison with null is defined:

static void Main()
    {
        Console.WriteLine($"null != null  => {null != null}");
        Console.WriteLine($"null == null  => {null == null}");
        Console.WriteLine($"null != true  => {null != true}");
        Console.WriteLine($"null == true  => {null == true}");
        Console.WriteLine($"null != false => {null != false}");
        Console.WriteLine($"null == false => {null == false}");
    }

and the results are:

null != null  => False                                                                                                                                                                                                                                  
null == null  => True                                                                                                                                                                                                                                   
null != true  => True                                                                                                                                                                                                                                   
null == true  => False                                                                                                                                                                                                                                  
null != false => True                                                                                                                                                                                                                                   
null == false => False

So you can safely use:

// check if null or false
if (nullable != true) ...

// check if null or true
if (nullable != false) ...

// check if true or false
if (nullable != null) ...

Solution 10 - C#

If you only want to test for true against null/false, One I've just used and reads quite well is

bool? someCondition = null
if (someCondition.Equals(true))
...

Solution 11 - C#

I think its up to you. I certainly think the .HasValue approach is more readable, especially with developers not familiar with the ?? syntax.

The other point of a nullable boolean type is that it is tristate, so you may want to do something else when it is just null, and not default to false.

Solution 12 - C#

Given enum

public enum PublishMode { Edit, View }

you can do it like here

 void MyMethod(PublishMode? mode)
    {
       var publishMode = mode ?? PublishMode.Edit;
       
//or
       if (mode?? PublishMode.Edit == someValue)
       ....
    }

Solution 13 - C#

If you're in a situation where you don't have control over whether part of the condition is checking a nullable value, you can always try the following:

if( someInt == 6 && someNullableBool == null ? false : (bool)someNullableBool){
    //perform your actions if true
}

I know it's not exactly a purist approach putting a ternary in an if statement but it does resolve the issue cleanly.

This is, of course, a manual way of saying GetValueOrDefault(false)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFireSnakeView Question on Stackoverflow
Solution 1 - C#Artiom ChilaruView Answer on Stackoverflow
Solution 2 - C#LuceroView Answer on Stackoverflow
Solution 3 - C#Dan DiploView Answer on Stackoverflow
Solution 4 - C#OdedView Answer on Stackoverflow
Solution 5 - C#Vladimir ShiyanovView Answer on Stackoverflow
Solution 6 - C#user1023602View Answer on Stackoverflow
Solution 7 - C#ZzeView Answer on Stackoverflow
Solution 8 - C#Andrey FrolovView Answer on Stackoverflow
Solution 9 - C#Sz. MonczView Answer on Stackoverflow
Solution 10 - C#ds4940View Answer on Stackoverflow
Solution 11 - C#James WestgateView Answer on Stackoverflow
Solution 12 - C#GopherView Answer on Stackoverflow
Solution 13 - C#MetalPhoenixView Answer on Stackoverflow