views:

63

answers:

4

When I say

public static IMyType GetGateWayManager()
{
    IUnityContainer _container = GetContainer();
    IMyType _gateWayManager = _container.Resolve<IMyType>();
    return _gateWayManager;
}

it comes with a warning saying use implicitly types local variable.

Now If I change it to

public static IMyType GetGateWayManager()
{
    IUnityContainer _container = GetContainer();
    var_gateWayManager = _container.Resolve<IMyType>();
    return _gateWayManager;
}

it is fine.

Anyone can tell why VS editor thinks it is best practice to lose type here

+1  A: 

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.

CodingInsomnia
But doesn't using `var` affect runtime perfermance ?
Itsik
@ltsik - No, it is no different to declaring the type manually.
Greg Beech
@Itsik, No, it is just syntax sugar and will be converted to the right type at compile time. It isn't a dynamic type, just a way of saving the programmer from writing out List<Dictionary<string, IEnumerable<int>>> etc.
Martin Harris
No. `var` is transformed into the correct type at compile time so behaves exactly the same, as opposed to `dynamic` in .net 4 which is only checked at runtime. If the type of `var` cannot be determined you still get a compile error.
Alex Paven
@ltsik: You may find this question useful http://stackoverflow.com/questions/3555625/var-reference-in-c-is-boxing/3555656#3555656
Brian Rasmussen
Thanks! I guess I should get on to Part 3 of `C# in Depth` (im almost finishing Part 2)
Itsik
A: 

It's Resharper warning you, not VS editor.

Giorgi
+3  A: 

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()

Eugene Cheverda
+3  A: 

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;
}
Greg Beech