2

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.

1
  • What is the rest of the code for that method? Commented Jan 8, 2010 at 9:43

6 Answers 6

13

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.

Sign up to request clarification or add additional context in comments.

Comments

10

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.

1 Comment

I think only this question is answering the question.
2

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.

1 Comment

(this is not something that's new to 3.5 btw)
1

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

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

A simple scenario is this:

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

If the compiler instantiated 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 always 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-instantiated object.

Comments

1

Supposing this is your code:

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

You are creating an instance of a DownloadMessagesReport in the first line. And then you throw this object away by assigning the dmrReceived variable another value returned from DownloadNewMessages method. The first new DownloadMessagesReport() object is redundant. You effectively creating garbage that Garbage Collector will have to clean at some point.

That's why ReSharper and StyleCop showing you warning.

If you can initialize variable with actual value right in the same line where the variable is declared then do it.

Comments

0

Surely this is enough?

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.