0
using System;

public class A{
    public bool func(){
        return true;
    }
    

    public int func2(){
        return 10;
    }
}

public class HelloWorld
{
    public static void Main(string[] args)
    {
        A a = new A();
        if(a?.func()){
            Console.WriteLine("true"); // Error
        }
        
        if(a?.func2() == 10){
            Console.WriteLine("true"); // print: True
        }
    }
}

Like above case, I want to use null conditional operator with A function that returns a bool value. But, It throws error only when used with bool returning function.

Can I know why it works like that?

Ironically, It works well with the phrase

if(a?.func() == true){
    Console.WriteLine("true"); // print: true
}
3
  • 2
    a?.func() simply does not return bool, but bool?. Commented Jan 5, 2023 at 16:01
  • Side note: if you'd really worked out the minimal reproducible example - bool? r = true; if (r) ... than you'd only need to read stackoverflow.com/questions/2673918/….... Commented Jan 5, 2023 at 17:03
  • 1
    a?.func() returns a bool?, the if condition requires a bool. The comparison operator == returns a bool, which is why it works. Commented Jan 5, 2023 at 17:25

2 Answers 2

3

Please, note that even if func returns bool

 a?.func()

returns nullable bool? (true, false and ... null):

 a        : a?.func()
 ------------------------------------------
 null     : null      
 not null : a.func() # either true or false

So you can put

 // if `a` is `null` then `a?.func() is null` 
 // since `null != true` the condition true if and only if
 // `a` is not null and `a.func()` returns true
 if (a?.func() == true)

here .net compares T? and T instances (T is bool in our case). It does it as follow

 // First compute the expression
 bool? result = a?.func()

 // Then .net checks if Value is not null
 // and if it is the Value equals to required value 
 if (result.HasValue && result.Value == true) ...

Another way is to use ?? to map null into true or false

 // in case of null - when `a` is null - assume false
 if (a?.func() ?? false)

The very same logic with

 if (a?.func2() == 10)

here a?.func2() returns nullable int? which you compare as == 10, i.e.

if a is not null and a.func2() returns 10 then...

Sign up to request clarification or add additional context in comments.

1 Comment

Good answer. But lacks an explanation why you can't use bool? in an if directly but as comparison(== true or == 10). Lifted operators
0

The if statement requires a boolean expression. That is, whatever is inside your parenthesis must resolve to either true or false. When you use the null-conditional operator it says, "give me the bool result of func() unless a is null, then give me null". So by adding the ?. you change your expression to a bool?. But as we established, the if statement requires a bool. if (null) is invalid which is what you get when a is null.

So why does if (a?.func() == true) work? By adding == true, you are now changing your expression back to a bool result type by doing a comparison. When a is null, instead of being

if (null)

you now have

if (null == true)

which is a valid boolean expression that resolves to false. So incidentally, the inverse is also valid if (a?.func() == false).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.