迭代器变量超出C#的范围
为什么我用C#写这个:
for(int myVar = 0; myVar<10; myVar++) { //do something here } //then,if I try: int myVar = 8; //I get some error that the variable is already declared. //but if I try: Console.WriteLine(myVar); //then I get the error that the variable is not declared. 我最常说的有点混乱.有谁知道为什么C#编译器会这样做? 解决方法
该规则以及其他几个相关规则将在
this blog post of Eric Lippert’s中讨论.
这里违反的特定规则是:
特别值得注意的是,关于规则存在的原因如下:
通过允许你所描述的内容,它可以产生看似良性的重构,例如将for循环移动到另一个声明之后,导致极其不同的行为. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |