Child Scope & CS0136

The following code cannot compile: "A local variable with the name" st "cannot be declared in this scope because it would give the value" st ", which is already used in the scope of" child "to mean something else" :

var l = new List<string>(); l.Find(st => st.EndsWith("12")); string st = "why this fails?"; 

I understand why this will not work:

  string preParent = ""; { string preParent = "Should fail cause we change the meaning"; } 

When we do the following, we get "CS0103: the name" postParent "does not exist in the current context":

  { string postParent=string.Empty; } postParent = "Should this work?"; 

I don’t understand why the compiler is smart enough to see that postParent is not in scope, but it will not allow me to define a new variable that has the same name as the variable used within the child scope (which is obviously out of scope of this paragraph).

Is the compiler a simple scope, refusing to let me use a variable? If so, that makes sense.

============

Edited by:

I think the interesting thing is how you can have the same variable in two child areas in the same method, so this is valid:

  { string thisWorks= string.Empty; } { string thisWorks= "Should this work?"; } 

I'm just curious that you can have two variables with the same name if they are on the same level (if you look at the area like a tree). This makes sense because you can have local variables in two methods of the same class with the same name.

I'm just surprised that the compiler is able to differentiate and resolve this, while it will not allow the postParent variable. And is it a technical limitation or is it a design decision? This is what I'm really trying to get to -)

+22
scope compiler-construction c #
Nov 17 '08 at 20:18
source share
2 answers

Yes, the compiler implements the scope. Note that the scope of a variable is the lexical block of which it is a part, not only from the point of declaration, but also the entire volume.

The compiler complains because the postParent goes beyond its scope (these are just nested curly braces). If you tried to declare a new variable in the place where you are currently assigning postParent , the problem will be with the nested block, since the postParent will include this nested block, even if it was before the declaration.

The areas are described in section 3.7 of the C # 3.0 specification.

EDIT: edit your question.

These are just two simple rules:

  • you cannot declare a local variable when another local variable with the same name is in scope
  • the scope of a local variable is the block in which the declaration occurs.

I’m sure that the language could be designed in such a way that the scope was just beginning at the point of declaration, but I think it’s easier (from the point of view of the complexity of the language) to consider areas as just blocks, so all local variables declared in for example are one and the same block has the same scope. This makes life a lot easier when looking at captured variables, since capture depends on the scope, and nested areas make life interesting ...

EDIT: The language specification should say about the original example of a lambda expression - section 7.14.1:

Optional anonymous function-signature Anonymous function defines the names and, if necessary, types of formal parameters of the anonymous function. The scope of the parameters of the anonymous function is the anonymous function of the body. Together with a list of parameters (if specified), the body of the anonymous method is a declaration space. For this reason, it is a compile-time error for the parameter name of the anonymous function corresponding to the name of the local variable, local constant, or parameter in the scope of the anonymous method expression or lambda expression.

Does it help?

+12
Nov 17 '08 at 20:24
source share

You declare a variable in a limited scope and try to use it outside that scope. The compiler assumes that you do not need access to it, so you can declare a variable with the same name elsewhere in the file. Your attempt to perform an old trick involving a variable will immediately go beyond scope. For example, this was used to work in older versions of C / C ++, but it no longer works.

 for (int i=0; i<10; i++) { cout <<"In the loop i is "<< i << endl; } cout << "outside of the loop i is " << i << endl; //this only compiles with old C/C++ compilers. 
-2
Nov 17 '08 at 20:28
source share



All Articles