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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With