Resharper: vars

后端 未结 8 2116
闹比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: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.

提交回复
热议问题