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?
!= nullis 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...if(name?) ...as opposed toif(name!=null)if(name)and it validates fornull, seemed to me that something close would be a nice thing and that it might be in the new update, guess not.??and?together can give you a concise expression. Ex:var bar = foo?.wombat ?? defaultWombat;