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

后端 未结 12 576
梦如初夏
梦如初夏 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.

提交回复
热议问题