Demystifying C# 3.0 - Part 1: Implicitly Typed Local Variables "var"

Posted on 6/30/2006 @ 8:40 PM in #Vanilla .NET by | Feedback | 14281 views

I am going to publish a series of blogposts that intend to bring C# 3.0 down to earth. C# 3.0, along with LINQ, and all the heavy duty talk that surrounds it has sort of made it difficult to understand IMO. Well - if not difficult to understand, it sure is tough to gauge - "Where to begin".

So with 5 minutes a day, a short post a day will dice and slice a single feature, and we will logically move to a fuller picture of C# 3.0, followed by LINQ, and DLINQ (if I still have steam left).

So here we go, with the first in that series, the "var" keyword.

In C# 2.0, you can declare an integer (or anything for that matter of fact) as -

int i;

You could also write something like -

int i = 1;

Generally speaking -

<datatype> <variablename> = <initializer> ;

Okay good. The important thing to realize is that "i" is an integer. In C# 3.0, the below is a valid statement -

var i = 1;

But what is "var"? "var" is .. umm .. a keyword, that results in "i" being of the same data type, of the initializer ~ in this case an integer. So, var i = 1; will result in creating a variable called "i", whose data type is "integer".

But then what is the difference between "var", "object" and "variant" (from COM or VB6 days).

Variants were oink oink piggies, basically a catch all pig that occupied way too much memory. Objects have boxing unboxing issues. Both Variants and Objects are not strongly typed.

Note that "i" is strongly typed to an integer—it is not an object or a VB6 variant, nor does it carry the overhead of an object or a variant. To ensure the strongly typed nature of the variable that is declared with the var keyword, C# 3.0 requires that you put the assignment (initializer) on the same line as the declaration (declarator). Also, the initializer has to be an expression, not an object or collection initializer, and it cannot be null. If multiple declarators exist on the same variable, they must all evaluate to the same type at compile time.

You could also create "arrays" of "vars" as follows -

var intArr = new[] {3,1,4,1,5} ;

Great !!! I hope this clarifies a bit what "var" is, in my next post, we will see what "Anonymous Types" are.
 

Sound off but keep it civil:

Older comments..


On 9/6/2006 4:00:46 PM Rip Ryness said ..
What's the point of "var". Seems to me it's less readable and has no advantages. I'm sure I must be missing something.


On 9/6/2006 4:04:23 PM Sahil Malik said ..
Rip,

"var" is programmer convenience. In fact, something like LINQ would be grossly unusable without "var". If you think about it - "var" enables you to write

select new {x,y} and an anonymous type is created for you.

Now if you wanted to change that to

select {x,y,z} - a different anonymous type is created for you.

But if anonymous types (and thus "var") wasn't around - you'd be stuck in a hell of declaring classes everytime you sneezed in C# vNext.


On 6/10/2007 2:02:21 AM Pranav said ..
'var' reminds of VB / ASP with its horrendous 'strName' kind of naming culture. Do they really need to promote this?


On 11/14/2007 6:26:18 AM Michael Sync said ..
but why do we need this "var"? what wrong with "int i"?


On 11/14/2007 7:08:33 AM Sahil Malik said ..
Michael, LINQ would be unusable without anonymous types.


On 11/14/2007 9:07:51 AM Michael Sync said ..
Sorry. Sahil. this is me again.. :) I got a few questions now..

1. How can I initialize the "Implicitly typed local variables" as "long" datatype? e.g. long v = 1;

2. What about "int vs uint", "double vs float vs decimal"??


On 11/14/2007 11:42:28 AM Sahil Malik said ..
Answer to 1.

var monkey = (long)2 ;

Answer to 2.


umm .. I really didn't understand the Q :-)


On 1/4/2008 1:04:25 AM Me said ..
just like it should be done.

var i = 1;


var i_long = 1L; // a long

var j = 3.14;


var j_float = 3.14f; // a float


On 6/20/2008 10:58:02 AM Michael Sivers said ..
People are mixing Anonymous Types and Implicitly Typed Local Variables in these comments. They have similar constructs but behave in different ways (variants and strongly typed respectively). This is my issue with the syntax for Implicitly Typed Local Variables; they are too similar to anonymous types and leads to confusion and legibility of code.


On 9/4/2008 1:14:33 AM Amit said ..
Sir, Plz can u tell me detail description on whts diff betweeen -


int x;


int x=new int();

and


dataset ds = new dataset

etc. etc.

Plz