locked
Use Implicitly Typed Variable Declaration RRS feed

  • Question

  • Hello,

    We recently upgraded to VS2008 and the 3.5 Framework.  So, I'm the process of getting used to all the new shinnies.  Well, to the point...  I use ReSharper and it kept giving me this message... 

    "Use Implicitly Typed Variable Declaration"

    ...so after some research I found what all the fuss was about for example...

    MyClass myVar = (MyClass)myVar2

    vs.

    var mVar = (MyClass)myVar2

    ...according to what I read in most cases this is optional and a convenience.   Now, I may be opening a can of worms here but which way is the more accepted practice.  I tend to like the look of the first one but I would rather move forward into 3.5 land doing this according to the proper conventions.

    Thanks,

    Jeff

    Wednesday, May 19, 2010 4:52 PM

Answers

  • This is the best answer for this question. I agree with his answer completely. I found this on stackoverflow a while back when I had the same question.

    I personally only use “var” when I can clearly distinguish the variable Type by just reading the declaration, for example:

    var someVariable = new List<int>();

    In the example above, its evident that “var” refers to “List<int>”.

    I don’t like to use “var” when I have to go to some method definition to find out what variable type “var” represents or by having to rely on visual studio intelli-popup or whatever that is called, for example this in not ok to me:

    var someVaraible = SomeMethod();

    I mean, what is the “SomeMethod” function supposed to return? Can you tell just by looking at the line of code? No you can’t, so that is why I avoid using “var” on those situations.

    Wednesday, May 19, 2010 4:54 PM
  • I use ReSharper too and this warning is one of the few that I routinely ignore (in fact, I recommend supressing this warning altogether so that it doesn't draw your attention away from far more important ones).

    The quote Mr. Crump included seems to make a lot of sense. There is nothing best praciselike about constantly using Implicit variable declarations. They don't save you large amounts of memory nor do they make your code weekly typed or less ambiguous (quite the opposite). At best following this practice will save your a few keystrokes... not worth it.

    This is one of those cases where ReSharper is being too you-know-what.

     

    • Marked as answer by JeffWask Thursday, May 20, 2010 5:02 PM
    Wednesday, May 19, 2010 6:00 PM
  • Complain to ReSharper about this default behavior - it's simply not a good idea to use implicitly typed variables everywhere you can.

     


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    • Marked as answer by JeffWask Thursday, May 20, 2010 5:02 PM
    Thursday, May 20, 2010 2:55 AM
  • this in not ok to me:

    var someVaraible = SomeMethod();

    Unless SomeMethod returns something you cannot declare. For example, if you have some method like this:

    List<T> NewList(param T[] values) { return new List<T>(values); }

    and you call it with anonymous type values:

    var myList = NewList(new { X = 1, Y = 2, Name = "Some name" }, new { X = 3, Y = 4, Name = "Another name" });

    In fact, that's what var is made for.

    • Marked as answer by JeffWask Thursday, May 20, 2010 5:02 PM
    Thursday, May 20, 2010 1:19 PM

All replies

  • This is the best answer for this question. I agree with his answer completely. I found this on stackoverflow a while back when I had the same question.

    I personally only use “var” when I can clearly distinguish the variable Type by just reading the declaration, for example:

    var someVariable = new List<int>();

    In the example above, its evident that “var” refers to “List<int>”.

    I don’t like to use “var” when I have to go to some method definition to find out what variable type “var” represents or by having to rely on visual studio intelli-popup or whatever that is called, for example this in not ok to me:

    var someVaraible = SomeMethod();

    I mean, what is the “SomeMethod” function supposed to return? Can you tell just by looking at the line of code? No you can’t, so that is why I avoid using “var” on those situations.

    Wednesday, May 19, 2010 4:54 PM
  • I use ReSharper too and this warning is one of the few that I routinely ignore (in fact, I recommend supressing this warning altogether so that it doesn't draw your attention away from far more important ones).

    The quote Mr. Crump included seems to make a lot of sense. There is nothing best praciselike about constantly using Implicit variable declarations. They don't save you large amounts of memory nor do they make your code weekly typed or less ambiguous (quite the opposite). At best following this practice will save your a few keystrokes... not worth it.

    This is one of those cases where ReSharper is being too you-know-what.

     

    • Marked as answer by JeffWask Thursday, May 20, 2010 5:02 PM
    Wednesday, May 19, 2010 6:00 PM
  • Complain to ReSharper about this default behavior - it's simply not a good idea to use implicitly typed variables everywhere you can.

     


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    • Marked as answer by JeffWask Thursday, May 20, 2010 5:02 PM
    Thursday, May 20, 2010 2:55 AM
  • this in not ok to me:

    var someVaraible = SomeMethod();

    Unless SomeMethod returns something you cannot declare. For example, if you have some method like this:

    List<T> NewList(param T[] values) { return new List<T>(values); }

    and you call it with anonymous type values:

    var myList = NewList(new { X = 1, Y = 2, Name = "Some name" }, new { X = 3, Y = 4, Name = "Another name" });

    In fact, that's what var is made for.

    • Marked as answer by JeffWask Thursday, May 20, 2010 5:02 PM
    Thursday, May 20, 2010 1:19 PM
  • Thanks for the Feedback.
    Thursday, May 20, 2010 5:02 PM
  • this in not ok to me:

    var someVaraible = SomeMethod();

    Unless SomeMethod returns something you cannot declare. For example, if you have some method like this:

    List<T> NewList(param T[] values) { return new List<T>(values); }

    and you call it with anonymous type values:

    var myList = NewList(new { X = 1, Y = 2, Name = "Some name" }, new { X = 3, Y = 4, Name = "Another name" });

    In fact, that's what var is made for.

     

    That's a very good point, Louis. I would also like to point out that not only is this what var was made for but is also the only thing it should be used for. Because, otherwise, there is a very real danger of running into type safety issues when the programmer successfully instantiates a variable in a way that would break the program once that variable is actually used, something that would have been caught by the compiler otherwise (There is a reason why C# was traditionally a strongly typed language.)

    Also, I'm pretty sure that the introduction of the keyword "dynamic" is .Net Framework 4 took away some of that usefullness too.

    Thursday, May 20, 2010 5:38 PM
  • A variable declare using the var keyword is still strongly typed. The only difference is that the compiler decides which type it stands for. The dynamic keyword is something else. That one actually makes a variable loosely typed.

    Friday, May 21, 2010 9:43 AM
  • Perhaps I wasn't being too precise (or, indeed, clear). I didn't mean to imply that after they are instantiated, variables declared with var are loosly typed. Just that not knowing what that type will be beforehand could cause type safety issues (and, as the result, make the whole thing, I don't know, less strongly typed as a whole if you understand what I really mean by "less here").

    As for the keyword dynamic, again, what I really meant is that while it was created for broader reasons, one could use the fact that the values are assigned in run time to assign, to use your words  "something you cannot declare", thus replacing (maybe?) some of var's functionality. I'll need to look into this a little more.

    Friday, May 21, 2010 3:58 PM
  • dynamic should not be used for anonymous types. dynamic variables are not strongly typed.
    Friday, May 21, 2010 4:07 PM
  • I knew they weren't strongly typed but I think I understand the point you are making. Thanks.
    Friday, May 21, 2010 4:54 PM
  • Agreed!

    To supress this warning (Resharper 6.1):

    In VS, select the Resharper menu / Options

    Go to Code Inspection / Inspection Severity / Language Usage Opportunities

    Change from Suggestion to Do Not Show for

    Use 'var' keyword when initializer explicitly declares type

    and Use 'var' keyword when possible.

    Wednesday, August 22, 2012 6:40 PM