The var keyword in CSharp can be tremendously useful - but for some, it’s still a source of confusion and something to be avoided at all costs. Here’s my take on when you should use var - and when you should not.

What is “var”?

As a quick refresher for those The var keyword simply requests the compiler to work out the type of a local variable for itself.

Those with long memories might remember the variant data type of Visual Basic 6 - a container that could contain any kind of value at any time. Despite the similarity in naming, the C# var is very different. A local declared with var has a specific type; it’s just that the compiler works it out automatically.

Do use var with anonymous types

With the inclusion of LINQ in C# 3.0 as a part of Visual Studio, the language gained support for anonymous types. The var keyword was introduced to allow local variables to be used - without it, anonymous types couldn’t be stored and manipulated and the feature wouldn’t have bean much use.

If you’re writing a method that uses anonymous types, you must use var for your locals.

Do use var when the type is already obvious

When you new an instance and immediately assign it to a local variable, the actual type is already shown.

Compare this declaration that shows the Type explicitly:

List<Customer> oldCustomers = new List<Customer>();

with this declaration using var:

var newCustomers = new List<Customer>();

Repeating the type twice just gives readers more to read without actually adding more information.

Similar advice applies when you’re initializing local variables with primitive values or simple expressions:

var customerCount = 0;
var inRange = minimum <= value && value <= maximum;
var separator = ", ";

Don’t use var when you want to control the type

Remember that the var keyword asks the compiler to work out the type for you - if you’re in a situation where the exact type is important, specify the exact Type for clarity.

For example, if you’re writing code that needs to interop with another system, getting the types correct is vital.

int fieldType = 0x8056;
string fieldName = "Sample";

Don’t use var when it obfuscates the code

Sometimes the compiler is perfectly able to work out what the type should be, but it’s not obvious to readers of the code.

For example, using an Option Type isn’t (yet) idiomatic C#, so it’s helpful to explicitly show the type to aid in comprehension:

Option<Customer> customer = FindCustomerById(customerId);

Consider using var when the name is explicit

When the name of the local variable is sufficient to make the type obvious, consider using var to simplify the code.

var criteria = CreateSearchCriteria();
var matchingCustomers = FindCustomers(criteria);

Conclusions

The var keyword is a useful feature that can be used to improve the readability of code, and to reduce the amount of code that you write.

Comments

blog comments powered by Disqus
Next Post
Sharpen The Saw #9  25 Jul 2016
Prior Post
Sharpen The Saw #7  11 Jul 2016
Related Posts
Semantic Types Redux  05 Jun 2016
Semantic Types in C#6  27 May 2016
Property Enhancements for C#  20 Dec 2015
Language Extensions for C#  19 May 2014
When should methods be Static?  09 Oct 2012
Of Method Naming and more  29 Sep 2012
Someone needs an intervention  16 Dec 2011
CallerInfo in C# 5  08 Dec 2011
Regions in C#  16 May 2011
Lambda expressions and Block syntax  15 Apr 2011
More csharp posts »
Related Pages
July 2016 archive