private bool? _success;
public bool Success
{
get
{
return _success ?? (_success = false);
}
}
Why can’t the compiler figure out the right operand is always false, and requires me to cast it to bool?
The right operand is a
Nullable<bool>since you’re assigning tobool? _success. This can’t be implicitly cast to a bool, which is why the cast is required.If you remove the assignment, then it will work fine:
This works as the “false” is a
boolalready.However, your current code returns
_successafter assigning it a value offalse.As _successis abool?, the right hand operand is returningbool?, and notbool.