C# Is this initialiser really redundant?

c#, resharper, stylecop

Solution

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.

Problem

I have the following line of code: ``` var dmrReceived = new DownloadMessagesReport(); ``` StyleCop and ReSharper are suggesting I remove the redundant initializer. However if I replace it with ``` DownloadMessagesReport dmrReceived; ``` surely this will generate an object reference not set to an instance of an object? I am using .NET 3.5. Do you no longer manually have to instantiate objects? Next line that follows is: ``` dmrReceived = dc.DownloadNewMessages(param, param2, param3); ``` It's worth noting that `dc` is a class generated from a WCF service. So `DownloadNewMessages` is a WCF web service method.

Original source