Thursday, March 18, 2004 - Posts

Iterator Isolation and Filter Iterators in C#

If you have not started using the iterators created by Eric Gunnerson in his Tips & Tricks article published on MSDN beginning 2002 then you should definetly do this right now.

Finally, it's possible to delete items from the collection, you're currently enumerating with for each using the following code:

  foreach (string s in new IterIsolate(hash.Keys))
  {
    if ((int) hash[s] == 0)
      hash.Remove(s);
  }

And that's just the beginning...

How about iterating though a collection that just returns items you approved in a custom filter function? Watch this:

  public static bool MyFilter(object o)
  {
    if ((string) o == "A")
      return true;
    else
      return false; 
  }

  IterSelectDelegate selector = new IterSelectDelegate(MyFilter);
  foreach (string s in new IterSelect(test, selector))
  {

     ...
  }
}

You can download the latest version with a new cool iterators that are not mentioned in Eric's MSDN article here.

Enjoy!

with 0 Comments