C# Is this initialiser really redundant?

后端 未结 6 626
故里飘歌
故里飘歌 2021-01-15 14:40

I have the following line of code:

var dmrReceived = new DownloadMessagesReport();

StyleCop and ReSharper are suggesting I remove the redun

6条回答
  •  暖寄归人
    2021-01-15 15:19

    If it's a field, it will be automatically initialised to its default value - null for a reference type. Given the var however, I'm guessing it's not, and that you're actually instantiating it further down in your code anyway, thereby discarding the value you have instantiated here. You don't need to initialise a variable where it's declared. If you want to use var you do, but then I'd recommend you declare it where you actually first use it.

提交回复
热议问题