I wonder if it’s possible to add an object from one list to another list like this:
objectToRemoveList.Add(listOfObjects.ElementAt(i));
Then, I want to remove all object in the list like this:
objectToRemoveList.Clear();
I wonder, because it seems like the objects are still there!?
That won’t work. Let me explain it in a graphical way. Your objects exist somewhere in memory:
Your list
listOfObjectsonly contains a reference to each of these objects:If you add some of these items (let’s say
bandd) toobjectToRemoveList, you are also only adding a reference:By executing
objectToRemoveList.Clear, you are just removing all references there. Note howlistOfObjectsis completely unafffected:To achieve what you want to do, you can use the following loop:
That way, the references are actually removed from
listOfObjects, which is what you want: