views:

247

answers:

4

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.

+11  A: 

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.

David M
Ok, that makes sense, thanks David.
JL
A: 

"Do you no longer manually have to instantiate objects?"

Of course you need to "manually" instaciate objects, how would the compiler know when or where to instaciate it otherwise?

A simple scenario is this:

MyType x;

if ( EverythingWorkedOut )
    x = new MyType(params);
else
    x = null;

If the compiler instanciated it the first time, it would be redundant and more overhead in all code.

Don't trust ReSharper or any other Computer-Intelligent-Stuff over your own Instincts! They're not alwasy right you know.

Just a side note, you don't really need to do x = null; since it should be the default value of a non-instanciated object.

Filip Ekberg
Downvoter can please leave a comment :)
Filip Ekberg
+1  A: 

That will only generate an object reference error if dmrReceived is accessed before it is assigned. A lot of the times, the reason for resharper saying that an initializer is redundant is that the variable will always be assigned another value in every single possible execution path.

i.e.

DownloadMessagesReport dmrReceived;

...

if(condition) {
    dmrReceived = new DownloadMessagesReport();
} else {
    throw new Exception("oh no");
}

return dmrReceived.SomeProperty;

Accessing SomeProperty is the first place in the code where dmrReceived actually needs to have a value. As follows from the rest of the code, there's no way to get to that line of code without assigning it a value, therefore, the initial value that might have been assigned, would not be used in any execution path, and would thus be redundant.

David Hedlund
(this is not something that's new to 3.5 btw)
David Hedlund
+1  A: 

So your code is

var dmrReceived = new DownloadMessagesReport();
dmrReceived = dc.DownloadNewMessages(param, param2, param3);

The second line does not fill the object you created in the first line but it completely replaces that object. So the first assignment is not needed (as the first object is never used), which is what R# is warning about.

Hans Kesting