Why not use var everywhere?

using or not using "var" does not change other observable characteristics of the program, such as its performance.

The question of whether or not to use "var" hinges upon its effect on the human readers and maintainers of the code, not on its effect upon the compiled artefact.

Hava a look at this excellent article: https://docs.microsoft.com/en-gb/archive/blogs/ericlippert/uses-and-misuses-of-implicit-typing


It's definitely not a performance hit. var isn't actually a type, but a placeholder in your code, that means "I don't want to write out the type of this variable." In fact, if you hover over the text in Visual Studio, it will show a tool-tip indicating the type that you decided was too long to write!

In all seriousness, though, you should generally use var when it's clear what the type is from the code, so that other aren't confused when reading it.


Yes, use var everywhere.

I love var. It's saved me loads of keystrokes. It helps me code in a "use first" fashion, and it makes refactoring more powerful.

It doesn't matter what the type is named. Intellisense tells me what the type can do, that is all that matters.

Even if I knew the type name, it wouldn't help me much if intellisense is broken, because I wouldn't necessarily know what a specific method or property is called just from the type name alone.

Some specifics where var makes things better:

  • Calling a method (without knowing what it returns - especially if it is a generic type)

This one is big. I often don't remember what a method returns, but I know the name of that method. Having to pull the return type out of my head slows me down. I just write var, call the method with my inputs, and voila intellisense tells me what that return type is and what I can do with it.

// Imagine a method that creates a return type that gets some generic type from the call arguments
Tuple<TA,TB,TC,TD,TE> Combine<TA,TB,TC,TD,TE>(TA a, TB b, TC c, TD d, TE e);

// GOOD: Call this with var
var combo = Combine( "Some text", 42, true, new Dictionary<int, List<string>>(), "Other text");

// BAD: Without var
Tuple<string, int, bool, Dictionary<int, List<string>>, string> combo = Combine( "Some text", 42, true, new Dictionary<int, List<string>>(), "Other text");
  • Refactoring a return type

If I use var, the compiler immediately tells me where that type is being misused (perhaps the new return type doesn't have the same property names).

If I didn't use var, I would just get an error about failure to assign the type. Then, I would have to go change that type to the new type (every single place it was called) and then I finally get the warnings where that type is being used incorrectly.

var is one of the best things that ever happened to C#.

// BAD: No var
Dictionary<int,List<Tuple<int,bool,string>>> ahhhhThatWasDifficult = new Dictionary<int,List<Tuple<int,bool,string>>>();

// GOOD: With var
// I can think of a good name before writing this complex type
var validNameCountDictionary = new Dictionary<int,List<Tuple<int,bool,string>>>();

If I still haven't convinced you, well you have no choice anyway if you want to use:

  • Anonymous types
  • Linq

So, why not go all the way and use var everywhere.

I know it's obscure, but I'll even do this sometimes just so I can always use var and have a consistent look to my code:

var number = (int?) null;

Cause I love var.

P.S. I am a little sad that let is replacing var in Typescript/ES6, but Javasctipt var !== C# var

Tags:

C# 4.0

Var