Article Options
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. ( 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: 

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 Score9721
Sponsored Links