I found something surprising today..

I was seeing this code which was trying to remove an item from a list. This list object was derived from List<T> and did not override the collection based methods.

I noticed that the original code did not remove the item from the list even though it tried to:

listObject.Remove(listObject.Find(delegate(listItemObject p) { return p.MyProperty == x.MyProperty && p.MyProperty2 == x.MyProperty2; }));

This looked fishy, so I changed this code to use LINQ to objects to get a list of items to remove and then I removed the items one by one inside a for loop just to see whether it would work or not.

Surprise Surprise! - It did not work.

Finally, I had to use the RemoveAll() method passing in this condition as an anonymous method to make the code remove it.

I could not understand why it would not remove the item when I called remove on an object which I got from within the list itself.

Comments

Popular posts from this blog

Enable Visual Studio to use more than 2GB of memory

Firefox and Chrome dark mode

Late 2008 Macbook only giving 1.5 gb/s speed with 6 gb/s Intel SSD?