Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Framework 3.5  »  Local Type Inference
Local Type Inference
by Roger McCook | Published  09/09/2008 | Framework 3.5 | Rating:
Roger McCook
Roger is an independent programmer in Atlanta, Georgia and president of McCook Software, Inc. (www.McCookSoftware.com). He focuses on Microsoft technologies including VB, C#, ASP.NET, and SQL Server. For the past eight years he has spent most of his time developing advanced systems for the telecom giant BellSouth (now a part of AT&T). Roger is a published author of many technical articles as well as fictional short stories. You can email him at: RogerMcCook@bellsouth.net 

View all articles by Roger McCook...
Local Type Inference

Type inference refers to the ability of a programming language to automatically deduce a variable’s type. Local type inference is available starting with C# 3.0 and Visual Basic 9.0, which were released in 2007 with version 3.5 of the .NET platform. The feature was added to support anonymous types and the relaxed programming style required by Linq. 

Local type inference lets you define and use local variables without specifying their type. These variables are commonly called implicitly typed local variables because the compiler infers a strong data type from the initialization expression. 

In Visual Basic, the compiler uses type inference to determine the data type without the "As" clause.  

' Using explicit typing
Dim x as Integer = 3

' Using local type inference
Dim x = 3

In C#, you use the var keyword as shown in the following example:

// Using explicit typing
int x = 3;

// Using local type inference
var x = 3;

Here are some things to keep in mind when working with local type inference:

  • Implicitly typed local variables are not the same as variables defined as objects. They are also not to be confused with variants (used in vb6).
  • Implicitly typed local variables must be initialized to a specific non-null value with the definition.
  • Their use is restricted. They are always local in scope. You cannot use them as a parameter in a function or in a member declaration or as a return type.  
  • You can't change the type.
  • In Visual Basic, local type inference is turned on by default but can be turned off using "Option Infer Off".
How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:2.83333333333334 out of 5
 12 people have rated this page
Article Score4059
Comments    Submit Comment

Comment #1  (Posted by Scout on 12/08/2011)
Rating
Phenomenal breakdown of the topic, you souhld write for me too!
 
Comment #2  (Posted by buy cheap oem software on 02/12/2012)
Rating
98CtVI It`s really useful! Looking through the Internet you can mostly observe watered down information, something like bla bla bla, but not here to my deep surprise. It makes me happy..!!
 
Comment #3  (Posted by esalerugs promo code on 02/13/2012)
Rating
I serched through the internet and got here. What a wonderful invention of the mankind. With the help of the network you communicate, learn, read !... That helped us to get acquainted!....
 
Comment #4  (Posted by cheapoair on 02/13/2012)
Rating
Strange but true. Your resource is expensive. At least it could be sold for good money on its auction!....
 
Comment #5  (Posted by followers exchange tool on 02/13/2012)
Rating
Not bad post, but a lot of extra !!....
 
Comment #6  (Posted by viagra on 02/13/2012)
Rating
Thank you, a very interesting note!!....
 
Comment #7  (Posted by Cherry Hill NJ personal trainer on 02/13/2012)
Rating
Stupid article..!!
 
Sponsored Links