Null-coalescing operator and operator && in C#
-
02-10-2019 - |
문제
Is it possible to use together any way operator ??
and operator &&
in next case:
bool? Any
{
get
{
var any = this.ViewState["any"] as bool?;
return any.HasValue ? any.Value && this.SomeBool : any;
}
}
This means next:
- if
any
is null thenthis.Any.HasValue
returnfalse
- if
any
has value, then it returns value considering another boolean property, i.e.Any && SomeBool
해결책
I'm wondering why nobody has suggested this so far:
bool? any = this.ViewState["any"] as bool?;
return any & this.SomeBool;
This returns
null
ifany
is null, no matter what the value ofthis.SomeBool
is;true
if bothany
andthis.SomeBool
are true; andfalse
ifany
is not null, andthis.SomeBool
is false.
다른 팁
Since you want to return null
in case the source is null
, I don't think ??
is going to help you write this any shorter or clearer.
Is this what you mean?
bool? Any
{
get
{
return ((this.ViewState["any"] as bool?) ?? false) && this.SomeBool;
}
}
I've left the return value as bool? but it looks like it could be changed to just bool.
This was tested like this:
class Program
{
private static readonly Dictionary<string, object> ViewState = new Dictionary<string, object>();
private static bool SomeBool;
static void Main(string[] args)
{
ViewState["any"] = (bool?)null; SomeBool = true; Console.WriteLine(Any);
ViewState["any"] = (bool?)false; SomeBool = true; Console.WriteLine(Any);
ViewState["any"] = (bool?)true; SomeBool = true; Console.WriteLine(Any);
ViewState["any"] = (bool?)null; SomeBool = false; Console.WriteLine(Any);
ViewState["any"] = (bool?)false; SomeBool = false; Console.WriteLine(Any);
ViewState["any"] = (bool?)true; SomeBool = false; Console.WriteLine(Any);
Console.ReadLine();
}
static bool? Any
{
get
{
return ((ViewState["any"] as bool?) ?? false) && SomeBool;
}
}
}
which returns
False
False
True
False
False
False
The behaviour here is not quite the same as the original as null should be returned for test cases 1 and 4 to be identical. But maybe that behaviour isn't required?
I think what you're trying to do is this:
return any ?? (any.Value && this.SomeBool) ? true : new Nullable<bool>();
However, I think in cases like this, it's probably more clear to use an if block:
if ( !any.HasValue )
return (any.Value && this.SomeBool) ? true : any;
else
return any;
If any is null
, then you want to return true
or null
, right?
The Null Coalescing operator isn't going to work for how you've structured the logic for your method. Sure you could force it in there, but it's going to look ugly and just confuse whomever reads it.
I found the original code hard to read and understand, so refactored and removed the ternary operator to reveal intentions.
bool? any = this.ViewState["any"] as bool?;
if (any == null)
return null;
return any.Value && this.SomeBool;
Null coalescing is just nice shorthand and should be used judiciously
Person contact = Mother ?? Father ?? FirstSibling;
Is more intention revealing, and easier to read + maintain than:
Person contact = Mother;
if (contact == null)
contact = Father;
if (contact == null)
contact = FirstSibling;
thing is, you don't really want to use the ?? operator. Its meant to make it easy to avoid nulls, you actually want to keep nulls.
This has the behavior you describe:
return (any ?? false) && this.SomeBool