考虑以下代码:
class C
{
public int A { get; set; }
public int B;
public C(int a, int b)
{
this.A = A; // Oops, bug! Should be `this.A = a`. No warning
this.B = B; // Oops, bug! Should be `this.B = b`. `warning CS1717: Assignment made to same variable; did you mean to assign something else?`
}
}
A
并且B
几乎完全相同,但是有一个我会错过的错误。
有没有办法在编译时捕获第一个案例?
编辑:一些答案和评论想向我解释属性和字段不是一回事。我已经知道了。他们解释了为什么编译器在这里没有警告;我明白了。但是我写了一个bug,我不喜欢写bug。所以我的问题是“我怎样才能确保我永远不会再写这个错误? ”