0
List<object> obj = new List<object>();
List<objectTypeof_d> list = new List<objectTypeof_d>();

foreach (Object o in obj)
{
    d = new Object();
    d = o;
    list.add(d);
}

I am trying to understand that when in loop we declare d = new Object(); then every looping it create new instance of object.It is better coding or below is right.

List<object> obj = new List<object>();
List<objectTypeof_d> list = new List<objectTypeof_d>();

foreach (Object o in obj)
{
    d = new Object();
    d = o;
    list.add(d);
    d = null;
} 

It's just for understanding ignore syntax,or assume object add into list in a loop. after adding can we set it to null or not. For memory optimization only.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • 3
    After the new you change the reference obtained by the new assigning the variable d to the reference of o. Thus your new is useless and you could remove it. Hence your question is.....? – Steve Nov 01 '17 at 09:17
  • You´re iterating an empty list (`obj`). You have to put something into it. – MakePeaceGreatAgain Nov 01 '17 at 09:21
  • Just for understanding that is above loop right or set obj=null for dispose a object after use.Just that's it. – Dhruv Parmar Nov 01 '17 at 09:27
  • 2
    @DhruvParmar No, setting `something = null` is not the correct way to dispose of something. The garbage collector takes care of it. However, some things, like an SqlConnection among many others, use unmanaged resources which the garbage collector cannot affect - those things need to have their `.Dispose()` method called on them. – Andrew Morton Nov 01 '17 at 09:30

2 Answers2

2

new will allways create a new instance, be it in a loop or not. So in your case you´re creating multiple instances of Object and throw them away as you´re overwriting the only reference to that instance by the line d = o. I suppose you can simply remove that line and write this instead:

foreach (Object o in obj)
{
    list.add(o);
}  

Or even simpler:

list.AddRange(obj);

However as you never add anything to obj (bad name for a list BTW), the loop won´t execute at all.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
1

It seems that you haven't grasped the concept of references.
I suggest to read this Values vs Reference Types

In your first example you create a new instance of an object calling d = new Object(); This variable d references a memory area to store your object (I suppose you use Object here just for example).

Now the following line changes the value stored in d (a reference value) to the reference value stored in o. Thus the first reference is lost and ready to be garbage collected.

Said that, it is clear that your real optimization here, is to remove the variable d and the call to new object(). (And also the call to d = null; )

Anyway, also maintaining your actual code, setting the variable d = null; doesn't change your memory footprint. The real cleanup of the memory is executed when the garbage collector runs and this time is chosen by the system when there is need for that process to run.

It is often a bad idea to think "I need more free memory, let's force a garbage collector run".

Steve
  • 213,761
  • 22
  • 232
  • 286
  • 1
    "It is often a bad idea to think..." I would go further and say "It´s allways a bad idea to assume this". – MakePeaceGreatAgain Nov 01 '17 at 09:50
  • I agree with you. I have never called that GC.Collect but it is possible that an edge case exists. See for example [this answer from Jon Skeet](https://stackoverflow.com/questions/478167/when-is-it-acceptable-to-call-gc-collect) – Steve Nov 01 '17 at 09:54