Darj
Darj

Reputation: 1403

Why "var" keyword sets to certain type?

Why var a = 7; would set a type to a certain type (i.e. int instead of byte)? Are there any rules/defaults/checks made on the fly by C# compiler?

Upvotes: 3

Views: 238

Answers (7)

Simon Whitehead
Simon Whitehead

Reputation: 65087

The compiler treats any integer literal in your code, that does not have a suffix, as an int.

So this:

byte myByte = 255;

..is actually implicitly converting the int constant 255, to a byte.

That is why var is infered to be an integer.. because the compiler uses integer literals by default.

If you were to do this:

var a = 7L;

A would be of type long.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503599

It's not clear what you mean by "on the fly" - but the C# compiler simply follows the rules laid down in the spec. For a declaration of the kind:

var a = expression;

the type of a is the type of expression. The expression 7 is of type int, although it's also known to be a constant within the range of byte, allowing:

byte a = 7;

to compile. The availability of that conversion to byte doesn't change the type of the expression 7 though, so int is what the C# compiler uses for the type of a.

Note that I'd recommend against using var for constants like this. It ends up with code which can get pretty confusing around the boundaries of int, uint, long etc. var is meant to help with anonymous types, and also to help make code more readable. When it makes code less readable, just don't use it.

Upvotes: 14

cArn
cArn

Reputation: 195

It sets a to the expression type because var is a general type and since the CLR handles the memory management for you it needs and will determine the type of the expression and make the variable a that type.

Upvotes: -2

Ewen
Ewen

Reputation: 1028

The var keyword automatically sets the giving value to the type that the compiler can convert. Ex: var s = ""; contains a string and will be made a string.

Upvotes: 0

newfurniturey
newfurniturey

Reputation: 38456

Per the manual:

An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

and:

The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement.

In plain and simple terms, the compiler will check the lowest available type for the data assigned to the variable and will strongly-type said variable to that data-type.

Upvotes: 0

alanmanderson
alanmanderson

Reputation: 8230

There are defaults, I couldnt' tell you all of them off hand. Similar to if you call 5/7 that it defaults to integer division. but if you do 5/7.0 then it will do regular division. var just sets the type to be whatever is the type of the assigned value, in your case without a cast it is an integer by default.

Upvotes: 1

Ed Swangren
Ed Swangren

Reputation: 124790

var does not mean "determine the type at runtime", it means "determine the type using the result type of the expression on the right hand side of the assignment operator." It is determined at compile time.

Upvotes: 0

Related Questions