DevPinoy.org
A Filipino Developers Community

>>> First two to make 3 wins! <<<

How To: What does "var" exactly mean in C#?

More than a year ago when I was reading about C# 3.0  and all the goodness attached to it I saw this new keyword called "var". At first, I thought it was something similar to what Variant means in VB6 or something like the Object type in .NET which is a catch all type bucket for any object in .NET (since everything is derieved from System.Object right?). Upon further review, I realized that my initial assumption was wrong and that var is whole different beast after all.

So what does "var" really do? Well for starters, var is strongly-typed while Variant and Object are not. Variants occupy alot of memory space when used and Objects have boxing and unboxing issues. "var" on the other hand doesn't have this problems. To further understand lets look at the example below:

Assuming that you want to declare a variable x of type int and assigning it with a value of 5:

int count = 5;

Looking at the snippet above you'll get the idea that the format for a variable declaration is as follows:

<type> <name> = <value|initializer>;

The important thing here is to realize that "count" is an integer. Now replace int with var:

var count = 5;

What happens here is that var automatically determines the datatype of the value you are assigning to it which in turn makes your var variable of that datatype.

So what is "var" exactly? Think of it as a shapeshifter. It takes whatever value you assign to it and it becomes that value and that type.


Posted 01-22-2008 4:20 PM by keithrull
Filed under: , , ,

Comments

jakelite wrote re: How To: What does "var" exactly mean in C#?
on 01-22-2008 7:21 PM

very cool compiler magic indeed. imagine how much typing time it saves you if you use a lot of generic collections.  

entries that used to be like this

Dictionary<T, CreateInstanceDelegate<T>> typeCreateInstanceMap = new Dictionary<T, CreateInstanceDelegate<T>>();

are now compressed to this

var typeCreateInstanceMap = new Dictionary<T, CreateInstanceDelegate<T>>();

cruizer wrote re: How To: What does "var" exactly mean in C#?
on 01-22-2008 8:07 PM

i don't think it really is a shapeshifter. after all, once you compile the code, the compiler guesses what type to make of the "var" and it stays that way. it's just a way of removing redundancy in code, like before we used:

List<string> myList = new List<string>();

when it was totally obvious that myList should be a List<string> anyway. so it gets shortened to:

var myList = new List<string>();

"var" is just a placeholder for whatever type the compiler deduces at compile time ;)

jakelite wrote re: How To: What does "var" exactly mean in C#?
on 01-22-2008 11:02 PM

I agree with Sir cruizer. Shapeshifters change at will. While this one permanently takes the shape its given at compile time. It would be interesting to look at the IL generated where one will discover

var x = 9;

gets translated to

int x = 9;

by the compiler.

keithrull wrote re: How To: What does "var" exactly mean in C#?
on 01-23-2008 9:34 AM

Good point. The reason why i made the connection with shapeshifting is because of the ability of vars to determine the type and properties of a specific object and in turn mimic it at compile time.

cruizer wrote re: How To: What does "var" exactly mean in C#?
on 01-24-2008 1:34 AM

well that's why it's commonly called "duck typing." if it quacks like a duck and walks like a duck then it's probably a duck.

Dennis wrote re: How To: What does "var" exactly mean in C#?
on 01-24-2008 1:12 PM

Isn't it type Inferencing which is denoted by the keyword var gets the type resolve at compile time while duck typing has it at runtime?


Copyright DevPinoy 2005-2008