Love when things work as you expect

I always love it when you think something should work one way and that’s exactly the way it works.  Had that happen to me today with a Stack object.

I wanted to iterate over a List and remove any objects that are null. Now I know I can do a reverse iterate and remove any nulls as I come across them without breaking the List, but I wanted to try to do it differently, just because 😬.

As I iterated the List, I pushed the index of any entry that was null onto a Stack.  Then when I was done, I could use Foreach on the Stack to pull out each index and remove it from the list.  As I’d used a Stack, the entries are fetched in the order of last-in, first-out, even in a Foreach loop.  Thought that might be the case, and was happy to find out it was.

Thought I’d share.

Leave a Reply