Home / ASP.NET Wiki / Visual Studio / Visual Studio 2008 / New "var" keyword

New "var" keyword

 Rate It (2)

The var keyword is a new keyword in Dot net Framework 3.5 that can infer its data type from the resulting set. Var keyword

For instance, look at the following examples:

var one = 1; //infers an int

var two = "2"; // infers a string

var three = from c in Customers select c; //infers IQueryable<Customer>

var four = customers.FirstOrDefault(); //infers Customer reference

This means full intellisense is available in VS 2008, as long as the compiler can infer the type.  The following results in a compile error:

var five = null;

because the compiler couldn't  infer the type of the variable from null. “Var” is NOT a variant, everything is known by the compiler at compile-time.

var keyword can be used in place of the type name when performing local variable declarations.  

Actually, "var" only tell the compiler to replace it with the type name by infering the type from the value we assign while initializing the variable.

Revision number 4, Tuesday, January 20, 2009 5:13:34 PM by vik20000in

Comments

Really very good short tutorial on var keyword.

Nice explanations for Novice people. thanks.

weake... need formating

Needs some mroe work got the basic idea but needs more examples maybe some more advanced examples to help explain bettter

Good work keep it up.

good one

I have never used this one...but try today.. Good article.!

Related Articles

Visual Studio 2008

Visual Studio 2008is theversion of Visual Studio which comes with the .NET Framework 3.5. It was released to MSDN subscribers on 19 November 2007 alongside the.NET Framework 3.5. Visual Studio 2008 can target developmentin .NET Framework versions 2.0, 3.0

Deployment

Deployment continues to get easier, but there's still a few gotchas here and there. Check out these choice articles for you deployment questions. Web Deployment Projects- Fritz Onion's most excellent article on Web Deployment Projects. He goes into

HTML

Visual Studio 2008 adds dramatically improved HTML and CSS Designer Support. From ScottGu: "VS 2008 now uses the same web designer that ships with Microsoft's new Expression Web product. In addition to providing lots of new functionality, you'll

Speed Up Visual Studio 2008

1- Disable toolbox populate from the mainmenu open Tools->Options Select the general tab under Windows Forms Designer Set AutoToolBoxPopulate to false. Although the setting is under Windows Forms Designer, it is just as effective for ASP.Net developers

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. abiruban (1)