When you execute TMPElementsX = TMPElements;
, you actually assign a reference.
It means that both variables TMPElements
and TMPElementsX
point at the same object. Removing an object using any of these variables will affect this object.
If you want it to be different and independent List
s with the same values then you can clone your List
.
If would be much easier if you had a List
of value-types (like int
, double
etc.). It would be this way:
List<int> TMPElementsX = new List<int>();
TMPElementsX = new List<int>(TMPElements); // new list created
ResultsElements.Add(TMPElementsX);
int ee = element;
TMPElements.Remove(ee);
However, in case of reference-types copy constructor creates the new list with the same referenced items. You will need to make a deep copy of a List. You can read some articles at StackOverflow to learn how to make a deep copy:
How do I clone a generic list in C#?
Generic method to create deep copy of all elements in a collection
In some cases, making a deep copy can be a bad choice. It's kind of hack but not solution. Probably, you can change your architecture and logics to work without it.