yosoy89
yosoy89

Reputation: 163

Iterator variable get out of scope in C#

Why if I write this in C#:

for(int myVar = 0; myVar<10; myVar++)
{
//do something here
}

//then, if I try:
int myVar = 8;
//I get some error that the variable is already declared.

//but if I try:
Console.WriteLine(myVar);
//then I get the error that the variable is not declared.

A little confusing I most say. Does anyone know why C# compiler does it?

Upvotes: 3

Views: 290

Answers (4)

Tearsdontfalls
Tearsdontfalls

Reputation: 777

Looking very strange, but C# offers you to seperate some code lines by simply using some curly brackets like this:

for(int myVar = 0; myVar<10; myVar++)
{
//do something here
}

{
   //then, if I try:
   int myVar = 8;
   //I get some error that the variable is already declared.

   //but if I try:
   Console.WriteLine(myVar);
   //then I get the error that the variable is not declared.
}

Upvotes: 0

phadaphunk
phadaphunk

Reputation: 13303

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

Both variables are declared in the same scope. (One in a child scope which still won't let you to declare it twice) You can't declare the variable a second time.
Changing the scope will allow you to declare it a second time.

private void myMethod()
{
   //Start of method scope 

   for (int myVar = 0; myVar < 10; myVar ++)
   {
      //Start of child scope
      //End of child scope
   }

   //End of method scope
}

private void anotherMethod()
{
   // A different scope 
   int myVar = 0 
}

Upvotes: 1

Servy
Servy

Reputation: 203820

This rule, along with several other related rules, is discussed in this blog post of Eric Lippert's.

The particular rule being violated here is:

2) It is illegal to have two local variables of the same name in the same local variable declaration space or nested local variable declaration spaces.

Of particular note, as to why the rules exist is the following paragraph:

the purpose of all of these rules is to 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. These rules are in particular designed to prevent nasty surprises when performing what ought to be safe refactorings.

By allowing what you've described it could result in seemingly benign refactors, such as moving the for loop to after the other declaration, to result in vastly different behavior.

Upvotes: 6

NSGaga
NSGaga

Reputation: 14302

The error you're referring to as already declared is quite descriptive and says...

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

myVar is actually not 'declared` when you try to use it the second time.

Upvotes: 0

Related Questions