Why should I use implicitly typed local variables?

Who are types for?

The compiler? Yes, absolutely. The compiler uses types to make it more likely that your program will function correctly at runtime by ensuring the types match up, you're calling methods that actually exist, and passing them parameters of the right type. Here, the compiler is checking that you're actually returning something of type IMyType.

The editor? Again, yes. The editor uses background compilation and type information to help you write code. When you hit . after _container it uses type information to tell you that there's a Resolve method and what parameters it takes.

You? Not so much. We've already seen that the compiler will ensure that you return something of type IMyType, so why do you care about declaring it as that type when the compiler can work it out and check it for you? Similarly, the editor will tell you about the methods on the container, so why do you care about declaring whether it's a Unity container or some other type of container, given you already know from the variable name it's a container of some kind and from the editor that it has a Resolve method.

There's no problem with declaring types for locals, but what ReSharper is telling you is that the compiler can work it out, so it's redundant information, and that your code could be clearer with implicit types and good variable names. For example, is the purpose of this code any less clear than the original sample?

public static IMyType GetGateWayManager()
{
    var container = GetContainer();
    var gateWayManager = container.Resolve<IMyType>();
    return gateWayManager;
}

It's probably not the VS Editor, but rather ReSharper that is giving you that message. It's more a matter of taste than of best practice. But once you get used to the var keyword, you start to like it more and more. At least I've learned to love it.


May be it's ReSharper.

ReSharper recommends using var when the type of variable can be seen in code. In your example we can see that _gateWayManager will be of type IMyType, then we use var keyword for implicit typing of variable. _container will be explicitly typed in code because we can't say object of what type will be returned by GetContainer()


Using var instead of explicit type is suggested by resharper because it is clear and useful.

Clear because you has less code written and your focus is on variable name instead of type name. You might think type name is useful, but after a short time you will just forget it.

Useful because when you change a method type return you won't need to change all types in the way.

Example:

int id = getId();
List<MyType> myList = FindById(id);

In this situation if you change id from int to guid you must change this "int" here. This is small, but easily can become big in real life projects. With var you have exacly the some code for compiler and do not need to change it always.

var id = getId();
var myList = FindById(id);

I used to prefer explicit types, but just few hours after try out var I won't let it so easily.

Remember: var is changed in compile time to correct type. It is different from dynamics which is not recomented in almost all cases.