Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>isn't this just plain wrong? </p> </blockquote> <p>No, this is not wrong at all. This is a correct implementation of section 7.5.2.1 of the C# specification, "Simple names, invariant meanings in blocks".</p> <p>The specification states:</p> <hr> <blockquote> <p>For each occurrence of a given identifier as a simple-name in an expression or declarator, within the local variable declaration space of that occurrence, every other occurrence of the same identifier as a simple-name in an expression or declarator must refer to the same entity. This rule ensures that the meaning of a name is always the same within a given block, switch block, for-, foreach- or using-statement, or anonymous function.</p> </blockquote> <hr> <blockquote> <p>Why is C# unable to differentiate between the two scopes?</p> </blockquote> <p>The question is nonsensical; obviously the compiler <em>is</em> able to differentiate between the two scopes. If the compiler were unable to differentiate between the two scopes then <em>how could the error be produced</em>? The error message <em>says</em> that there are two different scopes, and therefore the scopes have been differentiated!</p> <blockquote> <p>Should the first IF scope not be completeley seperate from the rest of the method?</p> </blockquote> <p>No, it should not. The scope (and local variable declaration space) defined by the block statement in the consequence of the conditional statement is lexically a part of the outer block which defines the body of the method. Therefore, rules about the contents of the outer block apply to the contents of the inner block.</p> <blockquote> <p>I cannot call var from outside the if, so the error message is wrong, because the first var has no relevance in the second scope.</p> </blockquote> <p>This is completely wrong. It is specious to conclude that just because the local variable is no longer in scope, that the outer block does not contain an error. The error message is correct.</p> <p>The error here has nothing to do with whether the scope of any variable overlaps the scope of any other variable; the only thing that is relevant here is that you have a block -- the outer block -- in which the same simple name is used to refer to two completely different things. <strong>C# requires that a simple name have <em>one meaning throughout the block which first uses it</em>.</strong></p> <p>For example:</p> <pre><code>class C { int x; void M() { int x = 123; } } </code></pre> <p>That is perfectly legal; the scope of the outer x overlaps the scope of the inner x, but that is not an error. What is an error is:</p> <pre><code>class C { int x; void M() { Console.WriteLine(x); if (whatever) { int x = 123; } } } </code></pre> <p>because now the simple name "x" means two different things inside the body of M -- it means "this.x" and the local variable "x". It is confusing to developers and code maintainers when the same simple name means two completely different things <em>in the same block</em>, so that is illegal.</p> <p>We do allow parallel blocks to contain the same simple name used in two different ways; this is legal:</p> <pre><code>class C { int x; void M() { if (whatever) { Console.WriteLine(x); } if (somethingelse) { int x = 123; } } } </code></pre> <p>because now the only block that contains two inconsistent usages of x is the outer block, and <strong>that block does not <em>directly</em> contain any usage of "x", only indirectly</strong>.</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. 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