← Back to context

Comment by breadwinner

21 hours ago

You are right about this not being a new feature. But if you don't use it, it seems like you haven't updated your skills in a while.

Besides making the code harder to read, "var" also makes your code less reliable as seen in this example: https://circles.page/567f44465c28b00bf8ed6cf9/Csharp-Type-In...

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

The problem you see is independent from var.

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