• <legend id='U4WQ1'><style id='U4WQ1'><dir id='U4WQ1'><q id='U4WQ1'></q></dir></style></legend>
    <i id='U4WQ1'><tr id='U4WQ1'><dt id='U4WQ1'><q id='U4WQ1'><span id='U4WQ1'><b id='U4WQ1'><form id='U4WQ1'><ins id='U4WQ1'></ins><ul id='U4WQ1'></ul><sub id='U4WQ1'></sub></form><legend id='U4WQ1'></legend><bdo id='U4WQ1'><pre id='U4WQ1'><center id='U4WQ1'></center></pre></bdo></b><th id='U4WQ1'></th></span></q></dt></tr></i><div id='U4WQ1'><tfoot id='U4WQ1'></tfoot><dl id='U4WQ1'><fieldset id='U4WQ1'></fieldset></dl></div>
    • <bdo id='U4WQ1'></bdo><ul id='U4WQ1'></ul>

    <small id='U4WQ1'></small><noframes id='U4WQ1'>

    1. <tfoot id='U4WQ1'></tfoot>
      1. 为什么 C# 不允许我在不同的范围内使用相同的变量名?

        Why doesn#39;t C# allow me to use the same variable name in different scopes?(为什么 C# 不允许我在不同的范围内使用相同的变量名?)

            <i id='JrTmL'><tr id='JrTmL'><dt id='JrTmL'><q id='JrTmL'><span id='JrTmL'><b id='JrTmL'><form id='JrTmL'><ins id='JrTmL'></ins><ul id='JrTmL'></ul><sub id='JrTmL'></sub></form><legend id='JrTmL'></legend><bdo id='JrTmL'><pre id='JrTmL'><center id='JrTmL'></center></pre></bdo></b><th id='JrTmL'></th></span></q></dt></tr></i><div id='JrTmL'><tfoot id='JrTmL'></tfoot><dl id='JrTmL'><fieldset id='JrTmL'></fieldset></dl></div>
            • <tfoot id='JrTmL'></tfoot>
                  <tbody id='JrTmL'></tbody>
                <legend id='JrTmL'><style id='JrTmL'><dir id='JrTmL'><q id='JrTmL'></q></dir></style></legend>
              1. <small id='JrTmL'></small><noframes id='JrTmL'>

                  <bdo id='JrTmL'></bdo><ul id='JrTmL'></ul>
                  本文介绍了为什么 C# 不允许我在不同的范围内使用相同的变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  例如:

                  if ( this.IsValid )
                  {
                      Matrix matrix = new Matrix();
                  }
                  
                  Matrix matrix = new Matrix();
                  

                  编译器警告我说:

                  不能在此范围内声明名为'matrix'的局部变量,因为它会给'matrix'赋予不同的含义,后者已在'中使用'child' 范围来表示其他东西.

                  "A local variable named 'matrix' cannot be declared in this scope because it would give a different meaning to 'matrix', which is already used in a 'child' scope to denote something else.

                  这些变量不是在不同的范围内,所以无论如何我都无法从 if 语句之外访问第一个 matrix 吗?

                  Aren't these variables in different scopes, so I wouldn't be able to access the first matrix from outside the if statement anyway?

                  推荐答案

                  更新:以下 2011 年的答案对于早期版本的 C# 是正确的;在最近的版本中,描述答案的规则已从 C# 中删除.设计团队确定,即使在我大大改进了错误消息以更清楚地诊断问题之后,该规则也会导致开发人员产生更多的混乱,从而导致出现此类问题,而不是阻止错误程序所保证的情况.

                  UPDATE: The answer below from 2011 is correct for earlier versions of C#; in more recent versions, the rule described the answer has been removed from C#. The design team determined that the rule caused more confusion amongst developers leading to questions like this one than the buggy programs prevented would warrant, even after I greatly improved the error messages to more clearly diagnose the problem.

                  到目前为止给出的答案非常令人困惑.对问题的正确分析始于阅读错误信息.错误消息告诉您实际上是什么错误:

                  The answers given so far are very confusing. The correct analysis of the problem starts by reading the error message. The error message is telling you what is actually wrong:

                  不能在此范围内声明名为'matrix'的局部变量,因为它会给'matrix'赋予不同的含义,而'matrix'已在'子'范围内用于表示其他内容.

                  "A local variable named 'matrix' cannot be declared in this scope because it would give a different meaning to 'matrix', which is already used in a 'child' scope to denote something else.

                  仔细阅读.它准确地告诉您违反了 C# 的哪条规则,即不允许在同一范围内使用相同的名称来引用两个不同的事物.(实际上,错误信息有点错误;应该说局部变量声明空间",它说范围",但这很罗嗦.)

                  Read that carefully. It is telling you precisely which rule of C# is being violated, namely that you are not allowed to use the same name to refer to two different things in the same scope. (Actually, the error message is slightly wrong; it should say "local variable declaration space" where it says "scope", but that is pretty wordy.)

                  此规则记录在 C# 4.0 规范的第 7.6.2.1 节:简单名称,块中的不变含义.

                  This rule is documented in the C# 4.0 specification, section 7.6.2.1: Simple names, Invariant meaning in blocks.

                  (在重叠的声明空间中有两个同名的局部变量是非法的.编译器也可能报告该错误,但在这种情况下它会报告更一般的错误.)

                  (It is also illegal to have two local variables of the same name in overlapping declaration spaces. The compiler could be reporting that error as well, but it reports the more general error in this case.)

                  这些变量不是在不同的范围内,所以我无论如何都无法从 if 语句之外访问第一个矩阵?

                  Aren't these variables in different scopes, so I wouldn't be able to access the first matrix from outside the if statement anyway?

                  是的.该陈述是正确,但无关.这里的错误是在同一个局部变量声明空间中使用了相同的简单名称来引用两个不同的东西.

                  Yes. That statement is true but irrelevant. The error here is that the same simple name has been used to refer to two different things in the same local variable declaration space.

                  考虑这种情况:

                  class C 
                  {
                      int x;
                      void M()
                      {
                          x = 10; // means "this.x"
                          for(whatever)
                          {
                              int x = whatever;
                          }
                      }
                   }
                  

                  同样的交易.这里的错误是在外部声明空间中使用简单名称x"来引用this.x,而在内部声明空间中使用简单名称x"来表示局部变量".在同一个声明空间中使用相同的简单名称来引用两个不同的事物——记住,内部声明空间是外部声明空间的部分——两者都混淆并且危险,因此是非法的.

                  Same deal. The error here is that the simple name "x" was used in the outer declaration space to refer to this.x, and was used in the inner declaration space to mean "local variable". Using the same simple name to refer to two different things in the same declaration space -- remember, the inner declaration space is a part of the outer one -- is both confusing and dangerous, and is therefore illegal.

                  由于显而易见的原因,它令人困惑;人们有一个合理的期望,即一个名称在第一次使用它的声明空间中的任何地方都意味着相同的东西.这很危险,因为小的代码编辑容易改变含义:

                  It is confusing for obvious reasons; one has a reasonable expectation that a name will mean the same thing everywhere throughout the declaration space in which it is first used. It is dangerous because small code edits are prone to changing the meaning:

                  class C 
                  {
                      int x;
                      void M()
                      {
                          int x;
                          x = 10; // no longer means "this.x"
                          for(whatever)
                          {
                              x = whatever;
                          }
                      }
                   }
                  

                  如果第一次使用简单名称的声明空间不重叠,那么简单名称引用不同的东西是合法的:

                  If the declaration spaces in which the simple names are first used are not overlapping then it is legal for the simple names to refer to different things:

                  class C 
                  {
                      int x;
                      void M()
                      {
                          {
                              x = 10; // means "this.x"
                          }
                          for(whatever)
                          {
                              int x = whatever; // Legal; now the 
                          }
                      }
                   }
                  

                  如需了解更多信息以及有关油炸食品的有趣故事,请参阅

                  For more information, and an amusing story about fried food, see

                  http://blogs.msdn.com/b/ericlippert/archive/tags/simple+names/

                  这篇关于为什么 C# 不允许我在不同的范围内使用相同的变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                      • <tfoot id='M9UOc'></tfoot>

                        1. <i id='M9UOc'><tr id='M9UOc'><dt id='M9UOc'><q id='M9UOc'><span id='M9UOc'><b id='M9UOc'><form id='M9UOc'><ins id='M9UOc'></ins><ul id='M9UOc'></ul><sub id='M9UOc'></sub></form><legend id='M9UOc'></legend><bdo id='M9UOc'><pre id='M9UOc'><center id='M9UOc'></center></pre></bdo></b><th id='M9UOc'></th></span></q></dt></tr></i><div id='M9UOc'><tfoot id='M9UOc'></tfoot><dl id='M9UOc'><fieldset id='M9UOc'></fieldset></dl></div>
                        2. <small id='M9UOc'></small><noframes id='M9UOc'>

                            <tbody id='M9UOc'></tbody>
                            <bdo id='M9UOc'></bdo><ul id='M9UOc'></ul>
                            <legend id='M9UOc'><style id='M9UOc'><dir id='M9UOc'><q id='M9UOc'></q></dir></style></legend>