A commenter on yesterday’s post asked about the lack of
Dispose. Here’s a way of doing it:
The mighty Skeet posted the other day about the idea of using a single instance of
IEnumerable as a shared input for multiple threads (not that I realised at first, but I got there eventually).
Clearly the interface is no good for that purpose, because two operations (“check for existence of next item” and “read next item”) are exposed as separate methods/properties, so it can’t be made atomic. Jared Parsons has blogged a lot of times very readably about this.
This got me thinking, because I’ve noticed that I can often shrink an interface declaration down so it only has one method. And then it doesn’t need to be an interface; it can just be a delegate. That way you can implement it on the fly with a lambda expression. If you express it without any out/ref parameters, you don’t even have to declare a new delegate type. And if you have a
Tuple class (as in .NET 4.0), you don’t need to declare any new types – just higher order methods.