Blog - Title

Local Variable Type Inference

Local Variable Type Inference

  • Comments 4

[Blog Map]  [Table of Contents]  [Next Topic]

Local variable type inference is a feature in C# 3.0 where you can use the var keyword instead of explicitly specifying the type of a variable.  The C# 3.0 compiler makes the type of the variable match the type of the right side of the assignment.  There are places when using C# 3.0 where we must do type inference when declaring variables; there is no other way, so we must cover this topic here.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC
LINQ query expressions (and many standard query operators) return a type of IEnumerable<T> for some type T.  When using LINQ, it is often convenient to make the type parameter be an anonymous type.  An anonymous type's name is automatically generated by the compiler, but hidden from the program.  If you write a query expression that creates an anonymous type, then the results of the query expression are an IEnumerable of the anonymous type.  To declare a variable to hold the results of such a query expression it is necessary to use type inference.  Anonymous types are introduced further on in this tutorial.

Simple Example of Type Inference

The following are a few local variable declarations with initializers:

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

The above code is precisely the same as:

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

The var keyword tells the compiler to take the type on the right hand side, and use that type for the variable.  It specifically does not mean VARIANT, or Object.  The variables are strongly typed; the only difference is that the compiler is able to deduce the type from the initializer.

The topic on anonymous types shows examples where it is necessary to use type inference.

Limitations of Type Inference

Type inference can be used only for declaring variables with local scope. You can't declare the return value for a method using type inference, nor can you declare member variables of a class using it.

[Blog Map]  [Table of Contents]  [Next Topic]

Leave a Comment
  • Please add 8 and 1 and type the answer here:
  • Post
  • You didn't mention that VB 9.0 has local variable type inference with the Dim keyword and Option Infer On|Off.

    C# myopia?

    --rj

  • I see a lot of writing about the fact that var is an inferred type, not a variant. Which should mean it's safe to use, right?

    Well, I don't agree and still fear "var" for clean programming. Why? Because we avoid the "variable type" effect of a variant, but it the compiler INFERS the type, then it may infer something different from the developer. For instance:

    var d = 1.0;

    could declare d as a Decimal instead of a Double.

    Now, I have to admin that var is extremely useful for LINQ. I'd just don't rely on it for my business code...

  • How is Arnaud's comment addressed?  Resolving to Int versus Long could have some severe consequences.

  • One point that I would make regarding local variable type inference: it is for local variables only. For instance, you can't declare the signature of a method using type inference. Regarding the above example:

    var d = 1.0;

    In that case, type inference uses the rules that the C# compiler uses to determine types of literals, so the type is double, I believe.

    In general, after many months of using it, I reserve type inference for two cases:

    - where declaring a variable that is an anonymous type (or collection of them). In this case, there is no other option.

    - where declaring a parameterized type, and it is a fair amount of typing to explicitly declare the type, yet the typing doesn't add any readability to the code, for example:

    var z = new List<string>();

Page 1 of 1 (4 items)