DCSIMG
C# 3.0 var keyword - Maor's Blog

C# 3.0 var keyword

C# 3.0 has many-a-new features. This post will explain the 'var' keywork and the concept of Implicitly Typed Variables.

The var keyword is not a late-bound or un-typed variable reference. The var keyword always generates a strongly typed variable reference. The main idea is that the developer is not required to define the type of the variable at the time of declaration, but it is the task of the compiler to decide what type of the object the variable is. The compiler infer the type of the variable from the expression used to initialize the variable when it is first declared.

Important points about var keyword:

  • The type of the variable is defined by the value declared and decided at the compile time. (Size depends on the type of the value initialised)
  • Type casting is simple and handled by CLR.
  • Explicit functions for parsing to specific type.
  • Can be used to reference any type in C#.
  • The CLR actually never knows that the var keyword is being used.

What rules shuold you force to use the var keyword?

When you declare variable with the var keyword, you must to always do an initial value assignment.  This is because the var keyword produces a strongly-typed variable declaration. Hence, the compiler needs to be able to infer the type to declare based on its usage.   If you don't, the compiler will produce a compiler error.

   1: var vAge = 32; 
   2: var vHeight = 175.2; 
   3: var vName = “Maor David“;  

 

The compiler will infer the type of the "age", "height" and "name" variables based on the type of their initial assignment value. This means it will generate IL that is absolutely identical to the code below:

   1: int age = 32;
   2: double height = 175.2;
   3: string name = "Maor David";

You can also use it to other data types:

   1: foreach (var vTable in ds.Tables) 
   2: {
   3:     foreach (var vRow in ((DataTable) vTable).Rows) 
   4:     {
   5:  
   6:     }
   7: }

This is equal to:

   1: foreach (DataTable table in ds.Tables)
   2: {
   3:     foreach (DataRow vRow in table.Rows)
   4:     {
   5:     }
   6: }
Technorati Tags: , ,
Published 15 September 2007 04:27 PM by Maor David-Pur
תגים:, ,

Comments

# DrorEngel said on 15 September, 2007 06:04 PM

this example

  1: var vAge = 32;

  2: var vHeight = 175.2;

  3: var vName = “Maor David“;

is nice but very risky and have a vast impact of the application performance

not always the compiler will guess which type I want...

second, the type casting is not "cheap" in CLR time

# Maor David - The Blog said on 27 September, 2007 09:27 AM

How many times have you written wrapper methods for objects that in reality you wished were part of the

# Maor David's Blog said on 07 October, 2007 12:25 AM

LINQ to XML is a built-in LINQ data provider that is implemented within the System.Xml.Linq namespace

# Marv said on 18 December, 2007 10:30 AM

So the main benefit is for lazy programmers?

# Maor David-Pur said on 18 December, 2007 02:28 PM

NO....var keyword is great for using with Linq.

Also, there are two cases where "var" is very useful. The first: is with complicated generic types. The second: var is useful is with anonymous types. Since you can't name these types, you need to use var when you want to assign values of these anonymous types to local variables.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:

Search

Go

This Blog

News

    RSS

     

    Connect with Me

    Maor's Facebook profile  Follow Maor on Twitter  Maor's profile on Linkedin  Maor in FriendFeed 
           

Syndication