本文介绍了C#6.0 /只读自动属性/不变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为与C#6.0相关的仪器/静态分析器中存在很少的错误,并且只读自动属性:

是否有计划添加新语法?

I think there are few bugs in the instrumentation / static analysier related to C# 6.0 and read only auto properties:
Is there any plans to add the new syntax?

以下是一个例子:

Here is an example:

public WebHeaderCollection Header { get;  } = new WebHeaderCollection();

似乎无法检测到这是一个只读的自动属性,并且具有可证明的不变量 

It seems unable to detect that this is a read only auto property and would have a provable invariant of 

Contract.Invariant(this.Header != null);

那里是一个明显的解决方法:不要使用新语法

There is an obvious work around: don't use the new syntax

public ClassCtor()
{
    this.Header = new WebHeaderCollection();
}

public WebHeaderCollection Header { get; private set; } 

推荐答案


这篇关于C#6.0 /只读自动属性/不变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 19:54