I'm not sure if I've mentioned before how much I love delegates, but I do. I made a new friend today in the form of System.Predicate<T>, another powerful aspect of the generics support in .NET 2.
A Predicate is a delegate wraps a method that accepts an instance of the Teplate type and returns whether or not the specific instance matches whatever condition the predicate represents. Many of the framework classes in .NET 2 work with the Predicate<T> generic. One example might be a generic List<T>,
(...)public World(Device dev){_device = dev;WorldEntities = new List<Entity>(100);_removePredicate = new Predicate<Entity>(ShouldRemove);}(...)public List<Entity> WorldEntities;private Device _device;private Predicate<Entity> _removePredicate;(...)protected void RemoveDisposed(){WorldEntities.RemoveAll(_removePredicate);}protected bool ShouldRemove(Entity target){return target.IsDisposed;}(...)
That is the first use I came across, but obviously there are many other saucy uses for this. Obviously you could do something almost this good in .NET 1, but the strong typing of the parameter class is what makes it cool.