logoalt Hacker News

xnorswapyesterday at 9:27 AM2 repliesview on HN

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.

Replies

tcfhgjyesterday at 10:07 AM

I would rename `x` to `foos` and jump to the function/use IDE hints for the exact type when needed.

show 2 replies
lan321yesterday at 3:09 PM

I 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.