Hacker News new | ask | show | jobs
by zamalek 3031 days ago
> You or some other maintainer will need to know what that type is later when reading the code.

The Microsoft rule is to only use var when the type is obvious from the assignment. That essentially boils down to new, cast and anonymous types.

2 comments

That seems to go against the OP's example benefits of very complex types or actually impossible types. The former will not be obvious from the assignment by nature of their complexity, no? The latter... well, being impossible without `var` implicit types is probably often also not obvious.
The "impossible type" (which is just anonymous) occurs within the same statement, so it is obvious.
Unless you are writing C# in notepad, just hover the mouse over the variable and you get the type. Even when there is an ambiguity, it's really a minor inconvenience to whoever is reading the code.