The var keyword in C# gives the programmer freedom to declare variables with implicit types. However, when to use it is a highly debated subject in the C# community. This post will look at the origins of the var keyword and our conclusion on when to use it.
Origins
Microsoft introduced the var keyword in C# 3.0, which allows declaring “implicit” variable declarations. However, the variables declared with the var keyword are still strongly typed. The difference is that you don’t need to declare it yourself; the compiler determines the type.
For example, the following code would not compile:
var i = 10; i = "x";
When i is declared, the type is determined to be an integer, so the string assignment fails with the following error:
Compilation error (line 7, col 7): Cannot implicitly convert type 'string' to 'int'
For the same reason, as you can imagine, you cannot just declare a variable with var type such as:
var i;
which would fail with the following error:
Implicitly-typed local variables must be initialized
Usage
Var keyword mainly has two usages:
- Declare anonymous types
- Not repeat type name in a variable declaration and object instantiation
The first usage is non-debatable, meaning that var is your only option if you declare anonymous types. Below is an example of using the var keyword to declare an anonymous type:
var person = new {Id = 1, Name = "Jack", Age = 25 }
Another use case is a query expression where you select a new type.
var somePeople = from person in people where person.Age > 20 select new { person.Id, person.Name }
In the second example, we create a new type on the fly rather than returning an existing one, so we must use the var keyword.
The debate
The debate around when to use var revolves around the second usage, as the first one is mandatory if you need anonymous types. So, should we use it as a shortcut and skip type declarations? Does that make the code easier or harder to read?
For example, consider a complex variable declaration as shown below:
Dictionary<string, Dictionary<string, List<string>>> items = new Dictionary<string, Dictionary<string, List<string>>>();
We can declare the same variable with the var keyword:
var items = new Dictionary<string, Dictionary<string, List<string>>>();
In my opinion, the second option is a lot clearer and makes the code easier to read.
Now let’s take a look at another example:
var userService = new UserService(); var user = userService.GetUser(1);
What is the type of “user”? Without the help of the IDE we are using, there is no way to know for sure just by looking at the code above. It can be a User class, an IUser interface, or a subclass of User. At this point, we don’t know.
Conclusion
Here’s my rule of thumb on when to use var:
For example, to review the above example, I’d go with explicit usage:
var userService = new UserService(); User user = userService.GetUser(1);
But if I were declaring the variable with a new keyword, I’d go with var:
var user = new User();
The benefit of this approach is especially obvious with complex types like the dictionary shown above:
var items = new Dictionary<string, Dictionary<string, List<string>>>();
In this case, duplicating the type name doesn’t add more clarity.
I hope this article helps you decide when to use the var keyword in C#.