Resharper: vars

后端 未结 8 2089
闹比i
闹比i 2020-12-01 10:52

Why does Resharper want you to change most variables to var type instead of the actual type in the code?

相关标签:
8条回答
  • 2020-12-01 11:27

    By default, it will "green squiggle" declarations of this type :

    Person p = new Person();
    ^^^^^^
    

    Because of the repetition.

    It will also suggest (small green underscore) var when it can be inferred :

    Person p = repository.GetPerson(1);
    ¯¯¯
    

    In this case it can be infered because of the return type of the GetPerson method.

    As stated by Jon Skeet, you can disable these suggestions in resharper's options.

    0 讨论(0)
  • 2020-12-01 11:33

    I think it suggests you both ways. If you have a explicit type - you can change it to var. If you have var - can change it to explicit. Just to make it faster for you to change if you think it is appropriate of course.

    It might be good to use vars, for instance, for loop variables, when iterating a collection, so on, when the type is "implicit" for you (it is always implicit for compiler of course, when Resharper suggests it) and its absence doesn't make the code less readable. Also, I like it to shorten some declarations, which may grow quite long with generics. Like, IList(IDictionary(SomeType)) myVar = List(IDictionary(SomeType)) () would not loose much if you write "var" at the left side of the assignment.

    (Replace parantheses with angle brackets ;)

    Of course, I would try to use vars with care, to improve readability and not vice versa.

    0 讨论(0)
  • 2020-12-01 11:35

    Vars help to make the code more readable inside a method, especially if you use generics.

    As Jon says, it is just an option.

    0 讨论(0)
  • 2020-12-01 11:36

    For me, it is definitely worth the price...(even if I had to pay it myself). But it can slow down your VS. It can get really slow if you have files like 5000 lines of code.

    What I still don't get however is why I am the only one in the team using it...

    0 讨论(0)
  • 2020-12-01 11:38

    This is the explanation on the JetBrains code inspection wiki about it: http://confluence.jetbrains.net/display/ReSharper/Use+%27var%27+keyword+when+initializer+explicitly+declares+type

    If you see the class to the right there is no big need to see it on the left as well. Also it saves space and reduces code if the class name is quite long. Personally I don't use var for simple types like string, int and so on but do use it for something like var dictionary = new Dictionary<string, int>() to save space.

    0 讨论(0)
  • 2020-12-01 11:39

    It's just an option. You can disable it:

    ReSharper -> Options -> Code Inspection -> Inspection Severity -> Code Redundencies -> Use 'var' keyword where possible: change this to "Do not show"

    There's also the context (lightbulb) option which will take you in each direction - this is under ReSharper -> Options -> Languages -> C# -> Context Actions -> "Replaces explicit type declaration with 'var'"

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