I tried to compile a class containing only the following method but it gives the error
"Error 1 A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else".
Why is it so
Please help..
public static void M()
{
{
char i = 'a';}
int i = 0;}

Why is this code giving a compile error?
Barry Bond
if ( something )
{
int i = 10; //A
Console.WriteLine( i );
}
int i = 10; //B
The compiler error given for the line marked B says:
Well, duh! No it won't! A variable is in scope from its point of declaration, so the 'i' introduced at B will NOT give a different meaning to the one introduced at A.
Grrrr!
johndog
Meryl Junik
Olivier Georg
i"is declared in the outer block and cannot be redeclared in the inner block. In this case C# behave like if "i" is a method: The method scope is the block in with you declarate it, do you can call to a method "b" from the method "a" declarated before.You can found the theory in http://msdn.microsoft.com/library/default.asp url=/library/en-us/csspec/html/vclrfcsharpspec_3_3.asp
Luis Forero
you declared a variable twice. If you made the first in a if or for statement it should work.
James Liddell
Tanvir
class="txt4" id="_ctl0_MainContent__ctl0_PostForm_ReplyBody"> int i = 20; //B
if ( something )
{
int i = 10; //A
Console.WriteLine( i ); // 10
}
Console.WriteLine( i ); // 20