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

后端 未结 12 545
梦如初夏
梦如初夏 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: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!

提交回复
热议问题