type inference var
Asked Answered
F

5

5

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?

Flavescent answered 15/11, 2012 at 14:42 Comment(3)
It's included to make LINQ queries and results easier to work with. Check this out: blogs.msdn.com/b/danielfe/archive/2005/09/22/472884.aspxEarlap
possible duplicate of Difference between "var" and "object" in C#Caaba
this question has been asked and answered numerous times beforeCaaba
N
7

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.

Nim answered 15/11, 2012 at 14:44 Comment(5)
Actually, @Joe, var is for "lazy typers."Scape
@Joe A "var" keyword is being changed to the "actual" datatype on compiling, isn't it? So it's only a difference when reading, not when executing the code.Narial
@Joe: Where did you get the idea that the compiler creates a wrapper? The compiler infers the correct static type and puts it in the IL, no more no less.Throckmorton
@Joe, Jon is right. Since .Net forces you to instanciate the object when using var it is strongly typed, and is primarily for lazy typing (and really-really long names). You cannot create a var as null, it wont build, which also means it can't magically become another type of object later.Devol
@JimMischel Well, lazy typers and also anonymous types.Assured
C
4

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.

Cisalpine answered 15/11, 2012 at 14:45 Comment(0)
E
1

What? No wow you got this all wrong.

C#'s var is nothing like VB's Variant, that's dynamic in C# (more or less).

var is just a placeholder for the compiler to fill in when it's compiling your program, at run time the actual type is fully declared and used, so there's absolutely no performance loss at all.

// this won't work
var i=0;
i="a"; // it would with dynamic though!
Edgington answered 15/11, 2012 at 14:46 Comment(0)
G
1

var is helpful for several reasons, it reduces verbosity of code as well as allowing for easier refactoring, when a type changes on the output of a method being assigned to a variable, if it still meets the usage no change is needed.

Also there are no performance hits as it gets compiled to IL where it ends up the same either way.

Gustavogustavus answered 15/11, 2012 at 14:46 Comment(0)
C
1

var does not degrade performance, and is still strongly typed.

It has been introduced because it was necessary for LINQ, since LINQ is able to return 'anonymous types'.
That was afaik the primary reason to introduce this keyword, since you have no control over the name of the type that will be generated by the compiler. An added value is, that you can use it as syntactic sugar to not duplicate the type of the variable when declaring one:

var = new List<MyComplexType<WithTypeParameter>>();

instead of:

List<MyComplexType<WithTypeParameter>> x = new List<MyComplexType<WithTypeParameter>>();

for example.

By all means, var is not something like the Variant datatype in Visual Basic. When you assign a certain instance to a variable that has been declared (and initialized) with the var keyword, you cannot assign an instance of another type to it. Next to that, you cannot declare a 'var' variable without initializing it.

Cashandcarry answered 15/11, 2012 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.