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

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

    @jongalloway - var doesn't necessarily make your code more unreadable.

    var myvariable = DateTime.Now
    DateTime myvariable = DateTime.Now;
    

    The first is just as readable as the second, and requires less work

    var myvariable = ResultFromMethod();
    

    here, you have a point, var could make the code less readable. I like var because if i change a decimal to a double, i don't have to go change it in a bunch of places (and don't say refactor, sometimes i forget, just let me var!)

    EDIT: just read article, i agree. lol.

提交回复
热议问题