请考虑以下代码:
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?`
}
}
Run Code Online (Sandbox Code Playgroud)
A并且B几乎完全相同,但有一个我会想念的错误.
有没有办法在编译时抓住第一个案例?
编辑:一些答案和评论想要向我解释,属性和字段不是一回事.我已经知道了.他们解释了为什么编译器在这里没有警告; 我明白了.但是我写了一个bug,我不喜欢写错误.所以我的问题是" 我怎样才能确保我永远不会再写这个错误? "
| 归档时间: |
|
| 查看次数: |
961 次 |
| 最近记录: |