Click here to Skip to main content
Click here to Skip to main content

Implicitly typed local variables

, 11 Nov 2010
Rate this:
Please Sign up or sign in to vote.
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)

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

 
Hey, I am Pranay Rana, working as a ITA in 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:


Follow on   Twitter   LinkedIn

Comments and Discussions

 
Generalsorry aspdotnetdev, you are wrong, and the first two are per... PinmemberDes Ward16-Nov-10 5:01 
GeneralVar has two purposes: storing anonymous types and being lazy... Pinprotectoraspdotnetdev11-Nov-10 7:12 
GeneralReason for my vote of 2 Using 'var' unnecessarily is a bad p... PinmemberSledgeHammer0111-Nov-10 5:18 
GeneralVar has good uses when you don't know the return type of the... PinmemberSilic0re0911-Nov-10 2:35 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 11 Nov 2010
Article Copyright 2010 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid