, , , , , .
:
string temp = data.val != null ? data.val.ToLower() : null;
if ("x".Equals(temp)) { }
.
:
bool? temp = data.val != null ? data.val.ToLower().Equals("x") : null;
if (temp) { }
This may be less obvious, but the operator data.val?.ToLower().Equals("x")
will return null
if data.val
is null
otherwise it will return the result data.val.ToLower().Equals("x")
. Since the operator can return null
or bool
, the type of the whole operator bool?
, but you cannot directly use bool?
in if
.
As others have pointed out, converting bool?
to bool
will fix your problem. One way to do this is to use GetValueOrDefault()
:
if (data.val?.ToLower().Equals("x").GetValueOrDefault()) { }
source
share