21

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.

If I change it to

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

it is fine.

Can anyone can tell me why the VS editor thinks it is best practice to use var here?

4
  • 4
    What is giving this warning? Is it the VS compiler or an external tool like ReSharper?
    – codeulike
    Aug 27, 2010 at 8:38
  • 1
    The fact that you say "lose type" suggests you could benefit from reading the documentation for var. It doesn't mean 'variant'.
    – AakashM
    Aug 27, 2010 at 8:48
  • 2
    definitely looks like Resharper warning... Aug 27, 2010 at 8:56
  • possible duplicate of Using implicitly typed local variables Oct 24, 2014 at 7:54

6 Answers 6

21

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;
}
2
  • 3
    There is an advantage to implicit typing, which is that if the return type of GetContainer changed (but kept the same signature) either because it was renamed, or replaced with a similar type, the above code wouldn't need changing to cope. (Although, it would have to be rebuilt)
    – Massif
    Mar 2, 2011 at 12:41
  • Yes, good variable names. What a great idea. Opacity_in_names_should_be_avoided.
    – Allen
    Jun 2, 2015 at 10:39
8

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.

1
  • 2
    So looking at your 2nd example, just by looking at the FindById method, what type is being returned? You can't tell, that's the disadvantage of implicit types.
    – LarryBud
    Feb 9, 2017 at 16:53
5

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

2

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.

5
  • 2
    @ltsik - No, it is no different to declaring the type manually.
    – Greg Beech
    Aug 27, 2010 at 8:45
  • 1
    @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. Aug 27, 2010 at 8:46
  • 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
    Aug 27, 2010 at 8:46
  • @ltsik: You may find this question useful stackoverflow.com/questions/3555625/… Aug 27, 2010 at 8:48
  • Thanks! I guess I should get on to Part 3 of C# in Depth (im almost finishing Part 2)
    – Itsik
    Aug 27, 2010 at 10:20
1

It's Resharper warning you, not VS editor.

0

The approved answer is certainly correct and answers the question as described.

But - I did want to bring up a case where you have to use implicit typing: when the variable represents an anonymous type.

Consider this code:

var upperLowerWords =
    from w in words
    select new { Upper = w.ToUpper(), Lower = w.ToLower() };

What type is upperLowerWords supposed to be? You can't say, because there is no explicit type that matches what is returned - but it is a type that the compiler can recognize. And, in fact, it does.

For reference: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables (which is where I got that example)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.