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.