T
Tony Johansson
Hello!
Below I have a very simple example that should cause this compile error but
compile fine.
Error 1 A local variable named 'x' cannot be declared in this scope because
it would give a different meaning to 'x', which is already used in a 'parent
or current' scope to denote something else
F:\C#\ConsoleApplication1\ConsoleApplication1\Program.cs 311 14
ConsoleApplication1
Here is how I think.
I have two WriteLine in this main as you can see.
The first one is writing 123 because I use the class name so this is
correct.
The second one is writing 1 but how can the compiler know that I don't want
the class name x instead.
Here is more.
If I change the first WriteLine so I just write x instead of Foo.x I get
this compile error
Error 1 A local variable named 'x' cannot be declared in this scope because
it would give a different meaning to 'x', which is already used in a 'parent
or current' scope to denote something else
F:\C#\ConsoleApplication1\ConsoleApplication1\Program.cs 311 14
ConsoleApplication1
I mean that the compiler wouldn't have the slighest idea which x I refer to
when simply writing x.
Can somebody explain this to me.
class Foo
{
static int x = 123;
static void Main(string[] args)
{
Console.WriteLine("x är {0}", Foo.x);
int x = 1;
Console.WriteLine("x är {0}", x);
}
}
Below I have a very simple example that should cause this compile error but
compile fine.
Error 1 A local variable named 'x' cannot be declared in this scope because
it would give a different meaning to 'x', which is already used in a 'parent
or current' scope to denote something else
F:\C#\ConsoleApplication1\ConsoleApplication1\Program.cs 311 14
ConsoleApplication1
Here is how I think.
I have two WriteLine in this main as you can see.
The first one is writing 123 because I use the class name so this is
correct.
The second one is writing 1 but how can the compiler know that I don't want
the class name x instead.
Here is more.
If I change the first WriteLine so I just write x instead of Foo.x I get
this compile error
Error 1 A local variable named 'x' cannot be declared in this scope because
it would give a different meaning to 'x', which is already used in a 'parent
or current' scope to denote something else
F:\C#\ConsoleApplication1\ConsoleApplication1\Program.cs 311 14
ConsoleApplication1
I mean that the compiler wouldn't have the slighest idea which x I refer to
when simply writing x.
Can somebody explain this to me.
class Foo
{
static int x = 123;
static void Main(string[] args)
{
Console.WriteLine("x är {0}", Foo.x);
int x = 1;
Console.WriteLine("x är {0}", x);
}
}