Archive

Archive for October, 2009

More on delegates vs interfaces

October 29, 2009 1 comment

A commenter on yesterday’s post asked about the lack of Dispose. Here’s a way of doing it:
Read more…

Categories: C#, delegates, functional Tags: , ,

Threadsafe Interfaces, Delegates vs Interfaces, Stuff Like That

October 26, 2009 3 comments

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.
Read more…

Categories: C#, functional, threads Tags: , ,

Zip and Tuple Neatness

October 23, 2009 3 comments

In BCL 4.0, there’s still no Unfold method (boooo), but at least there’s a Zip extension method to keep us warm as winter draws on.

I played with it thusly:
Read more…

Categories: BCL, C# 4.0 Tags: ,