Implicitly typed local variables, why local only?

R

Rene

Could someone tell me why is not possible to have Implicitly typed* field
level* variables? For example, why won't the following code compile?

class Program
{
var myString = "abc";

static void Main(string[] args)
{
}
}

If I try to compile this I get the following error: "The contextual keyword
'var' may only appear within a local variable declaration".

Why do I get that error? I mean, if I move the variable declaration inside
the "Main" method things compile perfectly fine so it's obvious that the
compiler is able to infer the type but why does it only work inside a
method?

Thanks.
 
A

Arne Vajhøj

Rene said:
Could someone tell me why is not possible to have Implicitly typed*
field level* variables? For example, why won't the following code compile?

class Program
{
var myString = "abc";

static void Main(string[] args)
{
}
}

If I try to compile this I get the following error: "The contextual
keyword 'var' may only appear within a local variable declaration".

Why do I get that error? I mean, if I move the variable declaration
inside the "Main" method things compile perfectly fine so it's obvious
that the compiler is able to infer the type but why does it only work
inside a method?

I would definitely not like a public field of type var - documentation
of it would be let us call it slightly frustrating.

:)

Arne
 
G

Göran Andersson

Rene said:
Could someone tell me why is not possible to have Implicitly typed*
field level* variables? For example, why won't the following code compile?

class Program
{
var myString = "abc";

static void Main(string[] args)
{
}
}

If I try to compile this I get the following error: "The contextual
keyword 'var' may only appear within a local variable declaration".

Why do I get that error? I mean, if I move the variable declaration
inside the "Main" method things compile perfectly fine so it's obvious
that the compiler is able to infer the type but why does it only work
inside a method?

Thanks.

The var keyword is mainly intended for use with anonymous classes, and
an anonymous class is limited to the method where it's created.

I think that the var keyword was limited to methods simply to reduce the
misuse of it. You should not use the var keyword for something as
trivial as declaring a string variable, as the code doesn't get more
readable from it.

In some cases it's useful, for example, instead of:

Dictionary<string, Dictionary<string, List<int>>> tree = new
Dictionary<string, Dictionary<string, List<int>>>();

you can use:

var tree = new Dictionary<string, Dictionary<string, List<int>>>();

Other uses may reduce the readability, for example:

var x = 42.0;

You need to know a bit about how literal values work, to realise whether
the type of the variable becomes a byte, short, int, long, float, double
or decimal.
 
J

Jon Skeet [C# MVP]

Could someone tell me why is not possible to have Implicitly typed* field
level* variables? For example, why won't the following code compile?

All of this is IMO:

There's no technical reason why it couldn't be done, but there's a big
difference between local variables and member variables, as all of the
uses of local variables are in the (hopefully short) piece of code
from their declaration to the end of their scope. In other words, you
have all of the context available at the same place. That makes it
less of a problem for it to be not-necessarily-absolutely-obvious what
the type is - you can emphasize the "what the code does" rather than
"here are the details of how it accomplishes it". The type of a member
variable is a bit more important, and it's harder to clearly see all
of its uses in the same place.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top