4 ms·
And I've seen style guides that forbid var unless the type is clear e.g. var example = new CustomClass(); and style guides the require it unless the code wi
by pc86 10y ago
And I've seen style guides that forbid var unless the type is clear e.g.
var example = new CustomClass();
and style guides the require it unless the code will actually break.
To quote the GP:
> Not saying that it doesn't happen. Every place has its weird "thing"
- nathanaldensr 10y agoThis 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>>();
- pc86 10y agoExactly 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.
- pjmlp 10y agoThat is one of the ways I usually use it, also auto in C++.
- jeswin 10y agoThe 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.