listCust...">

To the "new" or not to the "new",

Is there a rule when to use the new keyword, and when not when declaring objects?

 List<MyCustomClass> listCustClass = GetList(); 

OR

 List<MyCustomClass> listCustClass = new List<MyCustomClass>(); listCustClass = GetList(); 
+6
new-operator c # allocation
source share
9 answers

In your scenario, it seems that the actual creation of the object is done inside your GetList() method. Thus, your first sample will be the right use.

When you create your List<MyCustomClass> stored on the heap, and your listCustClass is just a reference to this new object. When you set listCustClass to GetList() , the reference pointer listCustClass discarded and replaced by the reference pointer to all GetList() returns (may be null). When this happens, your original List<MyCustomClass> is still on the heap, but there are no objects pointing to it, so it just spends resources until the garbage collector appears and clears it.

To summarize every time you create a new object, and then discard it, like the second example, you essentially lose memory by filling up a bunch of useless information.

+8
source share

In your second case, you create a new object in the first line, just to throw it in the second line. Totally not needed.

+43
source share

In this case, only your first example makes sense, because in the second case, you immediately replace the created list with the one returned by this method. Initializing a list to a new empty list makes sense when you add this list or when it is possible that the method you call to populate the list may somehow result in a null value if you would otherwise expect an empty list .

Examples where I can use initialization in 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; 
+18
source share

You use the new keyword to create a new instance of the object. Itโ€™s not clear from your question what the GetList method does, but it seems to either create a new list (thereby moving the new keyword somewhere else) or return an existing list (which someone created at one point with using new ).

+3
source share

The new keyword is mainly used to allocate space on the heap. If you create a value type (structs, etc.), you do not need to use a new keyword. However, control variables must be new'd before they are used.

In the above example, it seems that GetList () returns a link that is of type List, which would be created (new'd) somewhere inside the function. Therefore, in this scenario, the novelty is meaningless.

+3
source share

If you can embed it without losing the meaning and clarity of what you are doing, by all means, inline.

Edit: And since I build in regularly, I didnโ€™t even think about mentioning orphaned objects. Doh. =)

+2
source share

There is no rule, but itโ€™s common sense that you can apply most of the time.

An object must be created when it is created. Your GetList() function supposedly returns the (created) IList, so the second piece of code is completely unnecessary (you create an IList and effectively discard it on the next line).

The first fragment is completely suitable, however.

+1
source share

Do not think about it in terms of "should I use the new when I announce."

You use new when you assign (which may be part of the ad).

The first example is correct, the second is a waste of run-time resources.

0
source share

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

In this case, it seems that GetList () is using new to create a new list.

0
source share

All Articles