3 ms·
The guidelines I follow for C# has "use var where it's obvious, but explicitly type when not". So for example I'd write: var x = new List<Foo>(); Because w
by xnorswap 10mo ago
The guidelines I follow for C# has "use var where it's obvious, but explicitly type when not".
So for example I'd write:
var x = new List<Foo>();
Because writing:
List<Foo> x = new List<Foo>();
Feels very redundant
Whereas I'd write:
List<Foo> x = FooBarService.GetMyThings();
Because it's not obvious what the type is otherwise ( Some IDEs will overlay hint the type there though ).
Although with newer language features you can also write:
List<Foo> x = new();
Which is even better.
- tcfhgj 10mo agoI would rename `x` to `foos` and jump to the function/use IDE hints for the exact type when needed.
- Aeglaecia 10mo agoan interesting demarcation of subjective mental encapsulation ... associating the anonymous type of a buffer with the buffer's name ... as opposed to explicitly specifying the type of an anonymously named buffer
- Leherenn 10mo agoRight, although I would argue the most interesting part of the type here is the container, not the containee. With good naming it should be pretty obvious it's a Foo, and then either you know the type by heart, or will need to look up the definition anyway. With standard containers, you can have the assumption that everyone knows the type, at least high level. So knowing whether it's a list, a vector, a stack, a map or a multimap, ... is pretty useful and avoid a lookup.
- lan321 10mo agoI usually prefer List<Foo> x = new(); since it gives me better alignment and since it's not confused with dynamic. Nowadays I only use var x = new List<Foo>(); in non-merged code as a ghetto TODO if I'm considering base types/interface.