Implicitly Typed Local Variables are strongly typed variables that you define used the keyword var instead of the name of your type:
| var i = 5; |
The type is then inferred for you by the compiler by looking at the type of data that you populate the variable with.
WARNING. I have already had the situation where developers think that this feature is here to prevent them from using the conventional manner of instantiation anywhere in their programs. IT IS NOT! Whilst vars have their uses, you should not have to hover your mouse over a variable to know its type, no matter how “pretty” you think that your code looks! If you do not need to use a var then don’t use a var.
Implicitly Typed Local Variables have been introduced (IMHO) to enable anonymous types and to ease the use of LINQ. They can be used with the following types:
- Built-in types.
- Anonymous types.
- User-defined types.
- Any type defined in the .NET Framework class library.
They can be used in the following situations:
- With local variables. Var cannot be used for instance or class variables.
- In a foreach statement.
- In a using statement.
The examples listed below show the compilation-valid uses separated into what I would consider to be good and bad uses (the clue is in the method names):
| private void GoodUses() { // expr is compiled as IEnumerable<Customer> // or perhaps IQueryable<Customer> var expr = from c in GetCustomers() where c.City == “London” select c;// The type returned could be complex and hard to track down foreach (var customer in GetGroupedCustomers()) { if (customer.City == “London”) break; } } public void PoorUses() private void OnlyWayToDoIt() |
You cannot use Implicitly Typed Local Variables in the following cases:
- As an instance or class variable.
- As a return parameter in a method.
- As an input parameter to a method (unless used as input to a Lambda expression).
- When the variable is not declared and initialised in the same statement.
- When the variable is used in its own initialisation (although I’m not sure I’d be happy with any of my developers doing this anyway).
- In multiple defined variables in the same statement.
| var GetAsVar() { return 1; } // Cannot be a return parameter void SetAsVar(var v) { int i = v; } // Cannot be an input parameter private var _notAllowed; // Cannot be an instance variable private static var _alsoNotAllowed; // Cannot be a class variableprivate void NotAllowed() { var foo; // Must be initialised foo = “Hello”; var i = (i = 20); // Cannot be used in an initialisation statement |
A little GOTCHA is that if you’ve decided to name one of your types var for some reason then firstly shame on you and secondly, any attempt to use the keyword var will then result in the compiler attemtping to declare a variable of your type and NOT of an Implicitly Typed Local Variable.
More details can be found here.
Note that a lot of the code samples have come from Microsoft and as such I am obviously NOT taking credit for them.
To read about more C# 3.0 enhancements click here.
I’d hazard a guess that even:
int i = (i + 20);
won’t compile due to it being unassigned when you are trying to use it!
Matt
Try it. It does! Or rather “int i = (i = 20)” does, which is what I meant to type :-). I didn’t believe it though and it would never pass a code review but there you are, for some reason it does.
The example is lifted directly from the Microsoft documentation here, under remarks.