How to remove an object from List<T> in C# and return the removed object?
.net, c#, list
Solution
RemoveAt does not destroy the object and a deep copy will not be needed.
var result = list[i];
list.RemoveAt(i);
return result;
`RemoveAt` is O(n - i) vs `Remove` which is O(n) (where n is the length and i is the index of the element to be removed). After you've removed an element, you have to shift everything after it down one.
According to this documentation, if you want the most performant way of removing an element, swap it with last, element and then remove that. This does not maintain the order like the above. In theory this means that it is O(1) to remove the last element of a list.
var result = list[i];
list[i] = list[list.Length-1];
list.RemoveAt(list.Length-1);
return result;
Problem
How to remove an object from `List<T>` in C# and return the removed object ? Does `RemoveAt` destroys the object? Do I need to first make deep copy of the object and then call `RemoveAt`?