Re: simple example

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hello!

I still don't fully understand why this cause compile error.
I can't understand why not the first WriteLine use the class variable x and
the second will use the local variable x.
I think is very clear and can't understand why the compiler would complain.
class Foo
{
static int x = 123;

static void Main(string[] args)
{
Console.WriteLine("x är {0}", x);
int x = 1;
Console.WriteLine("x är {0}", x);
}
}


I can understand that this compile fine because you have explicitly stated
that the first WriteLine should use
the class variable x. So when you declared the local variable x this will be
used in the
second writeLine. So this is pretty easy to understand.
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);
}
}

This example is also easy to understand
static void Main(string[] args)
{
int x = 1;
{ int x = 1; }
}

//Tony

"Patrice" <http://scribe-en.blogspot.com/> skrev i meddelandet
news:OOFB2SsNKHA.2036@xxxxxxxxxxxxxxxxxxxxxxx
The OP tells actually that the code compiles fine but was expecting this
compile time error...

This is before this message is for *local variables* (which is what you
described). It doesn't apply to the OP case (i.e. he doesn't try to use
two local variable in the same scope but a local variable and a static
member that has the same name in a parent scope which is allowed).

This is further complicated as it seams the OP thought the location of the
local variable declaration inside the function is meaningfull (i.e. before
the declaration, x would refer to the static member, and after x would
refer to the local variable) when it is not...

I tried to elaborate in another response I just posted...

--
Patrice

"Alberto Poblacion" <earthling-quitaestoparacontestar@xxxxxxxxxxxxx> a
écrit dans le message de groupe de discussion :
e89x$xrNKHA.5108@xxxxxxxxxxxxxxxxxxxxxxx
"Tony Johansson" <johansson.andersson@xxxxxxxxx> wrote in message
news:h62sm.11612$U5.154437@xxxxxxxxxxxxxxxxxx
I mean that the compiler wouldn't have the slighest idea which x I refer
to when simply writing x.

In traditional versions of the C languaje and its derivatives, the
compiler always uses the version of x that is in the most internal scope
to the statement that uses it (for instance, it uses a local x instead of
an x declared as a member variable of the class).

As you have already noted, this can be confusing and can cause errors
when the variable that is being used is not the one that you had in mind
when you wrote the code. For this reason, the designers of C# chose to
forbid this situation, and therefore the compiler does not let you define
inside a scope a variable that has the same name as another variable in
an enclosing scope. This is why you get the error "A local variable named
'x' cannot be declared in this scope because it would give a different
meaning ..."







.



Relevant Pages

  • Re: question about assigning null to a reference object
    ... for local variables, then they are not the same. ... The following code will also fail to compile, ... it is best to not initialize it at all. ... them instinctively initialize all variables at their declaration. ...
    (comp.lang.java.help)
  • Re: already used in a child scope to denote something else
    ... The scope of a local variable declared in a local-variable-declaration ... is the block in which the declaration occurs. ... The scoping rules for local variables are designed to guarantee ... that the meaning of a name used in an expression context is always the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: simple example
    ... This is unrelated with static int x=123. ... So using x even before the declaration means that you are trying to use ... A local variable named 'x' cannot be declared in this scope because it would ... you still have two x local variables named the same way in ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Reusing the name of local variable
    ... it's not accessible before its declaration. ... Doesn't compiler see that these are two different variables? ... It considers them to be two different local variables with the same ... with one of them in the scope of the other. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: simple example
    ... This is before this message is for *local variables* (which is what you ... that has the same name in a parent scope which is allowed). ... the declaration, x would refer to the static member, and after x would refer ... and therefore the compiler does not let you define ...
    (microsoft.public.dotnet.languages.csharp)