views:

577

answers:

9

Is there a rule of thumb to follow when to use the new keyword and when not to when declaring objects?

List<MyCustomClass> listCustClass = GetList();

OR

List<MyCustomClass> listCustClass = new List<MyCustomClass>();
listCustClass = GetList();
+17  A: 

Only your first example makes any sense in this case since in the second case you are immediately replacing the created list with the one returned by the method. Initializing a list to a new empty list makes sense in the cases where you are adding to that list or when it is possible that the method you are calling to populate the list may somehow result in a null value when you would otherwise expect an empty list.

Examples where I might use initialization to a new, empty list.

List<MyCustomClass> listCustClass = new List<MyCustomClass>();
listCustClass.AddRange( GetList() );

or

List<MyCustomClass> listCustClass = new List<MyCustomClass>();
try
{
    listCustClass = GetList();
}
catch (SqlException)
{
}
return listCustClass;
tvanfosson
+42  A: 

In your second case you are creating a new object on the first line just to throw it away on the second line. Completely unnecessary.

erikkallen
I can't believe this is my by far most upvoted answer.
erikkallen
+2  A: 

If you can inline it without losing meaning and clarity of what you're accomplishing, by all means, inline.

Edit: And, as I regularly inline, I didn't even think about the orphaned object reference. Doh. =)

J. Steen
It'd be nice to know *what* the downvote was for. =)
J. Steen
It was vor Daniel's answer - just hit the wrong entry ... :D
Daniel Brückner
Ahaha. My colleague kept missing with his mouse all day, too. Must be monday. =)
J. Steen
+3  A: 

You use the new keyword to construct a new instance of an object. It is not clear from your question what the GetList method does, but presumably it is either creating a new list (thereby moving the new keyword somewhere else) or returning an existing list (which someone created at one point using new).

HTH, Kent

Kent Boogaart
The GetList should be using new to create a list so that it can return it to the caller. right? Is there any other way of implementing the GetList?
Bob Smith
The point is that someone is creating the list, whether it's GetList or some method that GetList calls.
Kent Boogaart
A: 

Don't think of it in terms of "should I use new when I declare".

You use new when you are assigning (which can be part of a declaration).

The first example is correct, the second is a needless waste of runtime resources.

Binary Worrier
+1  A: 

There is no rule of thumb, but there is common sense that you can apply most of the time.

An object needs to be instantiated when it is being created. Your GetList() function ostensibly returns a (created) IList therefore the second code snippet is quite unnecessary (You instantiate an IList and effectively discard it on the next line).

The first snippet is entirely appropriate, however.

Cerebrus
A: 

In C#, all instances of classes have to be created with the new keyword. If you are not using new in your current context, you either have a null reference, or you are calling a function that uses new to instanciate the class.

In this case, it appears that GetList() uses new to create a new list.

Josh G
+3  A: 

The new keyword is basically used to allocate space on the heap. If you are creating a value type (structs, etc...) you don't have to use the new keyword. However, reference variables have to be new'd before they are used.

In your above example, it seems as though GetList() is returning a reference that is of type List which would have been created (new'd) somewhere within the function. Hence in this scenario, new'ing is pointless.

Sirpingalot
+4  A: 

In your scenario it seems that the actual creation of the object is being performed inside your GetList() method. So your first sample would be the correct usage.

When created, your List<MyCustomClass> is stored in the heap, and your listCustClass is simply a reference to that new object. When you set listCustClass to GetList() the reference pointer of listCustClass is discarded and replaced with a reference pointer to whatever GetList() returns (could be null). When this happens your original List<MyCustomClass> is still in the heap, but no objects point to it, so its just wasting resources until the Garbage Collector comes around and cleans it up.

To sum it up everytime you create a new object then abandon it, like the second example, you're essentially wasting memory by filling the heap with useless information.

bendewey