Search code examples
c#.netgenericsgeneric-programming

.NET Generic List<T> Problem, works by design, need work around. Add value instead of reference


I have a List<T> and I need to avoid the behavior I'm about to outline:

    // assume cls and numberToAdd are parameters passed in.
    int pos = numberToAdd;
    List<MyClass> objs = new List<MyClass>(numberToAdd);

    for(int i = 0; i < numberToAdd; i++)
    {
        objs.Add(cls);
        objs[i].X = -((pos * cls.Width) + cls.Width / 2);
        pos--;
    }

    Console.WriteLine(objs[0].X + "\r\n" + objs[1].X);

This results in this writeline printing the same value.

Basically what I need is to change the behavior of the "Add" method. I'd like to add a new instance of the object with the same values and not simply a reference to the same object. I understand this will use alot more memory.


Solution

  • What is the 'cls' variable? Just create a new one inside each loop. What you want to do is clone it; but honestly that'll be confusing. I'd suggest just creating a new one per loop.

    -- Edit

    Noticed you'd comment about 'cls'.

    I suggest you clone it then (assuming you can't change the call and do my above suggestion). Do this by just creating a new one, and copying all the properties over.

    Typically, if you have control of the Type of 'cls', you just make a 'Copy' constructor. It's like so:

    class Foo {
        private int x;
    
        public Foo (Foo toCopy) {
           this.x = toCopy.x;
        }
    
        ...
    }