6

From what I gathered with the new null operator in C# 6.0 you can do something like this:

string name = people.FirstOrDefault()?.FullName;

And this is great, but one verification I often come across is something like this:

object name = null;
if(name != null) {
    DoSomething();
} else {
    DoSomethingElse();
}

Given the purpose of the new null operator, I would like something like this should be possible:

if(name?) {
    DoSomething();
} else {
    DoSomethingElse();
}

The problem here from what I understood is that when the value the ? is checking is in fact null, it returns null, and you need a bool condition for the if statement. Since you can't directly convert a null to a bool, is there a simpler way of checking this without doing if(name != null) using the new null operator in C# 6.0?

5
  • 12
    Seriously? Typing != null is not simple enough? It's clear, it's readable, the developer who looks at the code in six months can easily understand what it's doing... Commented Aug 25, 2015 at 15:17
  • 3
    What would be the purpose of such an operator? What is the benefit of writing if(name?) ... as opposed to if(name!=null) Commented Aug 25, 2015 at 15:18
  • 1
    In JS you can simply do if(name) and it validates for null, seemed to me that something close would be a nice thing and that it might be in the new update, guess not. Commented Aug 25, 2015 at 15:24
  • @ricochete there's nothing simple about this in Javascript - in fact inconsistent treatment of nulls it's one of the language's more problematic features. Why should a null value be treated as a boolean False? A missing value isn't logically the same as a False value. Commented Sep 1, 2015 at 10:10
  • Although this isn't what you asked for, do keep in mind that sometimes ?? and ? together can give you a concise expression. Ex: var bar = foo?.wombat ?? defaultWombat; Commented Mar 2, 2018 at 23:45

1 Answer 1

14

There's no point in doing this. You're not doing any null coalescing in your example - you're simply checking for null.

A better use would be if you wanted to check some member of the name object. For example:

if (name?.FirstName != null) ...

But as you can see, it's still a simple comparison for null - we just used null coalescing to avoid having to write something like this:

if (name != null && name.FirstName != null) ...
Sign up to request clarification or add additional context in comments.

4 Comments

I agree that it wouldn't be the better use of this feature, however I was going for something much simpler, something of the likes if(name) in JS, where it validates automatically for null. Thanks for the answer.
I like to do something like: if (Session?.Connected) { } but unfortunately this is not supported and you have to do something like: if (Session?.Connected == true) {}
@juFo That's kind of obvious, if you think about it - since you're using the coalescing operator, the bool is turned into bool? (the null has to propagate through all the null coalesces). And if (default(bool?)) makes exactly as much sense as allowing if (null) - which is to say, not a whole lot. What you can do is use (session?.Connected).GetValueOrDefault(false) - which is a lot more explicit than just a == true.
@Luaan or more concisely if (session?.Connected ?? false) - which still isn't any shorter or clearer than what juFo did. Starts to be more useful when more alernatives are wanted : if(session?.Connected ?? RestartSession()?.Connected ?? false)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.