Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type inference var

Type inference makes use of the var keyword. The compiler "infers" what the type of the variable is by what the variable is initialized to.
e.g. var somenum=o; becomes int somenum=0;

Even though somenum is never declared as being an int, the compiler figures this out & somenum is an int for as long as it is in scope.

it is like variant type used in visual basic. using it in program, upto some extent it degrades performance & var is not included in dot net framework before 3.5 .

even it degrades performance & dot net framework supports strong type checking ,why var is included in framework 3.5?

is var violets strong type checking? if not how?

like image 470
Ravindra Bagale Avatar asked Nov 15 '12 14:11

Ravindra Bagale


2 Answers

var does not degrade performance at all. The variable is still strongly typed:

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

The only difference is that you don't have to manually spell out the type in source code. There's no relation at all with VB 6's Variant, if that's what you are referring to.

like image 161
Jon Avatar answered Oct 24 '22 11:10

Jon


It is not like Variant at all, and it does not degrade performance.

In .NET, var is provided by the compiler as a shorthand mechanism; the compiled code is just as strongly-typed as if you had declared the correct type.

like image 30
Roy Dictus Avatar answered Oct 24 '22 10:10

Roy Dictus