Click here to Skip to main content
15,867,750 members
Articles / Programming Languages / C#
Tip/Trick

Implicitly typed local variables

Rate me:
Please Sign up or sign in to vote.
4.45/5 (10 votes)
11 Nov 2010CPOL 39.3K   2   4
Implicitly typed local variables

In an implicitly typed local variable declaration, the type of the local variable being declared is inferred from the expression used to initialize the variable. When a local variable declaration specifies var as the type and no type named var is in scope, the declaration is an implicitly typed local variable declaration. For example:


var i = 5;
var s = "Hello";
var d = 1.0;
var numbers = new int[] {1, 2, 3};
var orders = new Dictionary();

The implicitly typed local variable declarations above are precisely equivalent to the following explicitly typed declarations:


int i = 5;
string s = "Hello";
double d = 1.0;
int[] numbers = new int[] {1, 2, 3};
Dictionary orders = new Dictionary();

A local variable declarator in an implicitly typed local variable declaration is subject to the following restrictions:



  1. The declarator must include an initializer.
  2. The initializer must be an expression. The initializer cannot be an object or collection initializer by itself, but it can be a new expression that includes an object or collection initializer.
  3. The compile-time type of the initializer expression cannot be the null type.
  4. If the local variable declaration includes multiple declarators, the initializers must all have the same compile-time type.

The following are examples of incorrect implicitly typed local variable declarations:


var x;                      // Error, no initializer to infer type from
var y = {1, 2, 3};      // Error, collection initializer not permitted
var z = null;             // Error, null type not permitted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a Team Leadin MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog


StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr

Awards:



Comments and Discussions

 
Generalsorry aspdotnetdev, you are wrong, and the first two are per... Pin
Des Ward16-Nov-10 5:01
professionalDes Ward16-Nov-10 5:01 
GeneralVar has two purposes: storing anonymous types and being lazy... Pin
AspDotNetDev11-Nov-10 7:12
protectorAspDotNetDev11-Nov-10 7:12 
GeneralReason for my vote of 2 Using 'var' unnecessarily is a bad p... Pin
SledgeHammer0111-Nov-10 5:18
SledgeHammer0111-Nov-10 5:18 
Reason for my vote of 2
Using 'var' unnecessarily is a bad practice. Akin to people who litter code with the 'this' keyword. Var has its uses, but lazyness is not one of them.
GeneralVar has good uses when you don't know the return type of the... Pin
Ron Beyer11-Nov-10 2:35
professionalRon Beyer11-Nov-10 2:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.