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

后端 未结 12 549
梦如初夏
梦如初夏 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条回答
  •  萌比男神i
    2020-12-09 16:04

    No not always but I would go as far as to say a lot of the time. Type declarations aren't much more useful than hungarian notation ever was. You still have the same problem that types are subject to change and as much as refactoring tools are helpful for that it's not ideal compared to not having to change where a type is specified except in a single place, which follows the Don't Repeat Yourself principle.

    Any single line statement where a type's name can be specified for both a variable and its value should definitely use var, especially when it's a long Generic< OtherGeneric< T,U,V>, Dictionary< X, Y>>>

提交回复
热议问题