Re:Why Isn’t F# the King of .NET?
by nathanaldensr on Monday September 19, 2016 @08:03AM
(#12530661)
This is how I've had my ReSharper code formatting rules set up for years. I only use var when the type name explicitly appears on the RHS of the equal sign (e.g., in an explicit cast, a generic type parameter, a constructor, etc.)
var is an excellent way to reduce redundancy in an assignment expression; it helps avoid stuff like:
Dictionary<int, List<CustomClass>> dictionary = new Dictionary<int, List<CustomClass>>();Exactly how I use it (var) as well, only I haven't taken the time to tell ReSharper that's how I do it :)
Basically if I know what the type is by looking at that line of code, I will use var. If I don't, I won't.
The more widely held view is that var should be used as much as possible, as you can see in Resharper defaults. You needn't care about the types, if the compiler handles it for you. Code like int sum = getSum() needn't break if the getSum() implementation switched to using longs. There will be exceptions, of course.
People get by with dynamic types. Much better with structural types. You're having full nominal typing and using LHS declaration for documentation. Which if ever needed (typically you shouldn't), hovering over it gives you anyway.
To quote the GP:
> Not saying that it doesn't happen. Every place has its weird "thing"