I'm trying to remove a List<int> from a List<List<int>>, I've looked everywhere and haven't found a solution.
Here's what I've tried so far:
List<List<int>> my2DList = ... ; // this is where I assign my 2D list
my2DList.Remove(new List<int>( ... ));
But my2DList's length stays the same. What should I do?
The problem is that List<int> doesn't override Equals/GetHashCode, so your new list is never equal to the existing one. (Basically, it will be comparing references rather than contents.)
Three options:
RemoveRemoveAtRemoveAllAn example of the last one:
List<int> listToRemove = new List<int> { ... };
my2DList.RemoveAll(x => x.SequenceEqual(listToRemove));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With