标签:c language-design
Within a method, there can only be one object of any given name. We
got away with reusing the same variable names using the block level
scoping of our loop control variables in an earlier example, however,
an object of the same name outside of the block scope will show why
that does not work. See this example showing this naming conflict:
public static void DoWork()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
int i = 777; // Compiler error here
Console.WriteLine(i);
}
以上是从https://www.microsoft.com/net/tutorials/csharp/getting-started/scope-accessibility,我想问为什么会发生这种情况?为什么c#的设计是这样的,因为C和Java没有这样的东西.(我已经测试过,在Java和C中没有限制)
解决方法:
According to Eric Lippert,这个设计选择是为了
prevent the class of bugs in which the reader/maintainer of the code
is tricked into believing they are referring to one entity with a
simple name, but are in fact accidentally referring to another entity
entirely.
当你重构时,这种事情会特别让你感到困惑,而看似无辜的变化可以完全改变代码的含义.
标签:c,language-design 来源: https://codeday.me/bug/20190527/1165745.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。