c# - Using Elvis operator in combination with string.Equals -
i wonder why following code works in c# 6.0:
(in example data random class containing val public string)
if ("x".equals(data.val?.tolower()) { }
but following line isn't:
if (data.val?.tolower().equals("x")) { }
visual studio shows me following error:
cannot implicitly convert type 'bool?' 'bool'. explicit conversion exists (are missing cast?)
if ("x".equals(data.val?.tolower()) { }
will return boolean because of equals
call this:
if (data.val?.tolower().equals("x")) { }
when expression evaluated return system.nullable<bool>
different bool
(former struct can assigned value null
while latter can true
or false
) the if
expects. also, in c# null
value doesn't evaluate false (according c# specification).
Comments
Post a Comment