C# 6.0 Null Propagation Operator & Property Assignment

后端 未结 3 1951
情歌与酒
情歌与酒 2020-12-03 20:43

This question has been completely overhauled in the interest of being thorough in explanation.

I have noticed what appears to be quite a poor limitation of

3条回答
  •  长情又很酷
    2020-12-03 21:29

    You can't use the null-propagation operator in this way.

    This operator allows to propagate nulls while evaluating an expression. It can't be used as the target of an assignment exactly as the error suggests.

    You need to stick to the plain old null check:

    if (a != null)
    {
        a.Value = someValue;
    }
    

提交回复
热议问题