Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a frequently asked question; see also:</p> <p><a href="https://stackoverflow.com/questions/3133680">Lambda variable scope</a></p> <p><a href="https://stackoverflow.com/questions/2630244">C# going nuts when I declare variables with the same name as the ones in a lambda</a></p> <p><a href="https://stackoverflow.com/questions/2049330">C# variable scoping: &#39;x&#39; cannot be declared in this scope because it would give a different meaning to &#39;x&#39;</a></p> <p><a href="https://stackoverflow.com/questions/1196941">Variable scope confusion in C#</a></p> <p>The answer is: read the error message more carefully. The error message states exactly what the problem is: <em>you are not allowed to use the same simple name to mean two different things in the same block.</em> </p> <p>For example:</p> <pre><code>class C { int x; void M() { int x; } } </code></pre> <p><strong>Perfectly legal.</strong> Note that the scope of the outer x <em>overlaps</em> the scope of the inner x. It is <em>not</em> illegal to have overlapping scopes with the same name in scope in both.</p> <p>This is not legal:</p> <pre><code>class C { int x; void M() { Console.WriteLine(x); // this.x { int x; } } } </code></pre> <p>Again, the two scopes both with an x overlapping is perfectly legal. What is illegal is that the simple name x is used two mean <em>two different variables in the same block</em> - that is, within the outer block of M(), which <em>contains</em> the inner block of M.</p> <p>Programs where the same simple name is <em>used</em> to mean two different things in the same block are <em>confusing</em> and <em>bug prone</em> and therefore are <em>illegal</em> in C#.</p> <p>For more details read my articles on the subject:</p> <p><a href="http://blogs.msdn.com/b/ericlippert/archive/tags/simple+names/" rel="nofollow noreferrer">http://blogs.msdn.com/b/ericlippert/archive/tags/simple+names/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload