Remix.run Logo
recursive 4 days ago

SomeMethod().Fire(); has the same "problem". var e = new Employee(); does not.

The problem you see is independent from var.

breadwinner 4 days ago | parent [-]

You are right, SomeMethod().Fire() has the problem too. But typically you write

   Employee e = SomeMethod();
   e.Fire();
This does NOT have the same problem as var. When you use var you reduce the opportunities for the compiler to catch your bug. So the best practice is to explicitly state the type when possible. It makes the code more readable too.

What's more, Microsoft recommends using implicit typing for local variables only when the type of the variable is obvious from the right side of the assignment. See: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals...

But that's not what the community is doing, and some tools (JetBrains Rider) recommend using var whenever possible.

recursive 4 days ago | parent [-]

Microsoft recommendation is reasonable. I don't think var should be used as much Rider recommends. But var is perfectly fine in itself. The problem you illustrated occurs independently from var. You assert that one "typically" writes code a certain way but I've seen plenty of both. Further, sometimes you need var. Sometimes the target type can't be spelled like with anonymous types.

breadwinner 4 days ago | parent [-]

You are contradicting yourself... var is perfectly fine... but it shouldn't be used as much...

recursive 3 days ago | parent [-]

Sorry for being unclear. Var is perfectly fine in many cases. Sometimes there are better tools. Screwdrivers are fine tools but they are not appropriate for driving nails.