C# 6.0 Null Propagation Operator & Property Assignment

后端 未结 3 1954
情歌与酒
情歌与酒 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 21:20

    You're not the only one! SLaks raised this as an issue (now here)

    Why can't I write code like this?

    Process.GetProcessById(2)?.Exited += delegate { };

    and after it was briefly closed as "By design"

    the ?. Operator never produces an lvalue, so this is by design.

    someone commented that it would be good for property setters as well as event handlers

    Maybe add also properties setters into request like:

    Object?.Prop = false;

    and it was re-opened as a feature request for C#7.

提交回复
热议问题