Yes, use var everywhere.
I love var. This saved me from keystrokes. This helps me to code the “use the first” way, and it makes refactoring more powerful.
It doesn't matter which type is named. Intellisense tells me what a type can do, that's all that matters.
Even if I knew the type name, it would not help much if intellisense were broken, because I would not necessarily know which specific method or property is called only from the type name.
Some features in which var improves quality:
- Method call (not knowing what it returns, especially if it's a generic type)
This one is big. I often don’t remember how a method returns, but I know the name of this method. Getting 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 that this is a return type 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");
If I use var , the compiler immediately tells me where this type is used incorrectly (perhaps the new return type does not have the same property names).
If I did not use var , I would just get an error when assigning the type. Then I would have to change this type to a new type (each place was called), and then I finally get warnings that this type is being used incorrectly.
- Focus on the names of the names, not the types of type names.
var is one of the best things that has 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 have not convinced you, well, you have no choice if you want to use:
So why not go all the way and use var everywhere.
I know this is unclear, but sometimes I do it, so I can always use var and look at my code sequentially:
var number = (int?) null;
Because I love var.
PS I'm a little sad that let replaces var with Typescript / ES6, but Javasctipt var ! == C # var