Should I *always* favour implictly typed local variables in C# 3.0?

后端 未结 12 498
梦如初夏
梦如初夏 2020-12-09 15:48

Resharper certainly thinks so, and out of the box it will nag you to convert

Dooberry dooberry = new Dooberry();

to

var doo         


        
相关标签:
12条回答
  • 2020-12-09 16:12

    The best summary of the answer I've seen to this is Eric Lippert's comment, which essentially says you should use the concrete type if it's important what the type is, but not to otherwise. Essentially type information should be reserved for places where the type is important.

    The standard at my company is to use var everywhere, which we came to after reading various recommendation and then spending some time trying it out to see whether the lack of annotated type information was a help or a hindrance. We felt it was a help.

    Most of the recommendations people have linked to (e.g. Dare's one) are recommendations made by people who have never tried coding using var instead of the concrete type. This makes the recommendations all but worthless, because they aren't speaking from experience, they're merely extrapolating.

    The best advice I can give you is to try it for yourself, and see what works for you and your team.

    0 讨论(0)
  • 2020-12-09 16:12

    @jongalloway - var doesn't necessarily make your code more unreadable.

    var myvariable = DateTime.Now
    DateTime myvariable = DateTime.Now;
    

    The first is just as readable as the second, and requires less work

    var myvariable = ResultFromMethod();
    

    here, you have a point, var could make the code less readable. I like var because if i change a decimal to a double, i don't have to go change it in a bunch of places (and don't say refactor, sometimes i forget, just let me var!)

    EDIT: just read article, i agree. lol.

    0 讨论(0)
  • 2020-12-09 16:18

    "Best style" is subjective and varies depending on context.

    Sometimes it is way easier to use 'var' instead of typing out some hugely long class name, or if you're unsure of the return type of a given function. I find I use 'var' more when mucking about with Linq, or in for loop declarations.

    Other times, using the full class name is more helpful as it documents the code better than 'var' does.

    I feel that it's up to the developer to make the decision. There is no silver bullet. No "one true way".

    Cheers!

    0 讨论(0)
  • 2020-12-09 16:23

    There was a good discussion on this @ Coding Horror

    Personally I try to keep its use to a minimum, I have found it hurts readability especially when assigning a variable from a method call.

    0 讨论(0)
  • 2020-12-09 16:23

    There's a really good MSDN article on this topic an it outlines some cases where you can't use var:

    The following restrictions apply to implicitly-typed variable declarations:

    • var can only be used when a local variable is declared and initialized in the same statement; the variable cannot be initialized to null, or to a method group or an anonymous function.
    • var cannot be used on fields at class scope.
    • Variables declared by using var cannot be used in the initialization expression. In other words, this expression is legal: int i = (i = 20); but this expression produces a compile-time error: var i = (i = 20);
    • Multiple implicitly-typed variables cannot be initialized in the same statement.
    • If a type named var is in scope, then the var keyword will resolve to that type name and will not be treated as part of an implicitly typed local variable declaration.

    I would recommend checking it out to understand the full implications of using var in your code.

    0 讨论(0)
  • 2020-12-09 16:25

    I have a feeling this will be one of the most popular questions asked over time on Stack Overflow. It boils down to preference. Whatever you think is more readable. I prefer var when the type is defined on the right side because it is more terse. When I'm assigning a variable from a method call, I use the explicit type declaration.

    0 讨论(0)
提交回复
热议问题