Null Conditioning运算符不能用于赋值?

Viv*_*kla 3 c# c#-6.0

请查看以下代码并帮助我直观地了解我收到编译器错误的原因.

class Program
{
    static void Main(string[] args)
    {
        Sample smpl = GetSampleObjectFromSomeClass();

        //Compiler Error -- the left-hand side of an assignment must be a variable property or indexer
        smpl?.isExtended = true; 
    }
}

public class Sample
{
    public bool isExtended { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我是否应该推断出空调节仅用于访问属性,变量等而不是用于分配?

注意:我已经提到了类似的帖子(链接如下),但在我看来,没有进行足够的讨论.为什么C#6.0在使用Null传播运算符时不允许设置非null可空结构的属性?

编辑:我期待着类似的东西

If(null!= smpl) 
{ 
smpl.isExtended = true; 
}
Run Code Online (Sandbox Code Playgroud)

好像我的期望是不对的!

And*_*lon 5

你的扣除是正确的.空条件运算符仅适用于成员访问,而不适用于赋值.

也就是说,我倾向于同意语言/编译器应该允许运算符用于属性赋值(但不是字段),因为属性赋值实际上被编译为方法调用.

已编译的属性赋值如下所示:

smpl?.set_isExtended(true); 
Run Code Online (Sandbox Code Playgroud)

这将是完全有效的代码.

另一方面,很容易认为它会引入当前不存在的属性和字段使用之间的语法差异,以及使代码更难以推理.

您还可以访问codeplex上的大多数C#6.0设计说明.我做了一个粗略的扫描,无条件讨论跨越了许多部分.