Archive for the ‘C#’ Category

JavaScript Generators

February 8, 2010 2 comments

These work annoyingly well. Why annoying? Because they’re Mozilla specific. If only we could use them all the time. The other thing that’s annoying is how they are actually better than the C# equivalent.

There are two major advantages compared with C#’s yield return.
Read more…

Categories: C#, javascript, yield return Tags: , , ,

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: , ,

How var fixes a type hole in C#

August 5, 2009 1 comment

Assuming we have:

public class Base {}
public class Derived : Base {}
public class Unrelated {}

Read more…

Categories: C#, foreach Tags: , ,

Referential Identity and Internship

June 30, 2009 Leave a comment

I was thinking about a gotcha in Java, and started wondering about the idea of making it convenient to intern reference objects.
Read more…

Categories: .NET, C#, Interop, threads Tags: , , ,

Making Lambdas and Iterators Play Nicely Together

February 26, 2009 Leave a comment

The major problem with this idea is in my favourite feature of C#. In an iterator (a function containing the yield keyword), the code is transformed into a state machine represented by a class. It has to be chopped up into sections, so the function can be “paused” after each yield return. Those pieces must all be in the same function, so sadly lambdas don’t play well with iterators.

There are a couple of possible future language features which would completely solve this, however.

Read more…

Categories: C#, lambdas, yield return Tags: , ,

What’s Wrong With C# 4.0’s dynamic Keyword, and How I Think It Should Be Fixed

February 25, 2009 2 comments

Update: Exactly what I want, already implemented, complete with caching of the call site. Also, Microsoft’s Mads Torgersen responds to this suggestion here.

C# 4.0 proposes to add the dynamic keyword to the language. There are a few problems with it:

Read more…

Categories: C#, C# 4.0, dynamic Tags: , ,

Displaying a nested evaluation tree from Expression<Func>

February 13, 2009 2 comments

Updated: The downloadable source (see link below) is now tidied up a bit, and also displays any embedded string comparisons in a similar way to NUnit.

This might be useful as a way to write Assert statements in tests. Instead of requiring many different forms of Assert to capture values and intents, we could just have one Assert that accepts a lambda:

Read more…

A functional replacement for the using statement

February 8, 2009 Leave a comment

The using-statement is just that: a statement. Why does this bug me?

The FileStream object has a Length property. Assuming I have a function Open that returns a FileStream ready for us, it is awfully tempting to do this:

Read more…

Categories: C#, IDisposable, lambdas Tags: , ,

Further Muddying the ForEach Waters

February 3, 2009 3 comments

ForEach is regularly proposed as a missing feature in the BCL but has been rejected in the past apparently because it wouldn’t be "functional" enough, doesn’t return anything so calls cannot be chained together in a LINQ-like pipeline, and so on. So here’s a different approach that is undeniably functional, and does support chaining together.

Read more…

Categories: C#, delegates, foreach Tags: , ,

Get every new post delivered to your Inbox.