See more sharp things

I found a few interesting C# related things over the weekend.

On the open Roslyn codeplex repository there is now a link to a specification for pattern matching in C#. This boils down to a new operator, is, which can be called to determine if a pattern (named via the class on which it is defined) matches, and this operator can also provide a set of out variables that reflect any destructuring in the binding. The standard C# switch statement has been extended to allow cases to be defined that use patterns. The specification can be found here and there is already a mass of discussion here. Unfortunately there isn’t yet an implementation available (though one is promised in a few weeks), and it is always a lot easier to digest these things when you can play with examples.

Delegate and Func always appear similar but disconnected in the .NET framework. Your own delegate type is code generated to IL as a subclass of the inbuilt System.MulticastDelegate type, and the compiler goes out of its way to make the construction of such objects transparent – at its root, a MultiCast delegate is just an instance object (of type object, which can be null if you are referencing a static method) together with a token representing the method you wish to call.

However, you cannot simply convert from one to the other, so the xx3 line in the following code will not compile.

delegate string TestCode(int x);

static void Main()
TestCode xx = x => x.ToString();
Func<int, string> xx2 = x => x.ToString();
Func<int, string> xx3 = xx;

The trick as outlined in this post, is to use the Invoke method on the delegate instance.

Func<int, string> xx4 = xx.Invoke;

As you can see from the IL, this generates a new delegate instance that references the function token of the Invoke method.

L_0039: ldloc.0
L_003a: ldftn instance string ConsoleApplication6.Program/TestCode::Invoke(int32)
L_0040: newobj instance void [mscorlib]System.Func`2<int32, string>::.ctor(object, native int)
L_0045: stloc.2

I’m a big fan of Scala, mainly because the language contains a mass of rather cool language features such as call by name and multiple inheritance via Traits. Recently I did the online Coursera Scala course where there was a module on the for comprehension syntax and how it gets translated into a sequence of maps and filters. It was therefore interesting to see a blog post by Eric Lippert that discusses the more complicated translation that C# specifies for its translation of LINQ expressions.  It is rather interesting that overload resolution in C# has a number of issues when it comes to resolving nested lambda expressions which he references in the article.

As usual, when rereading bits of the Joe Duffy’s excellent Concurrent Programming in Windows,  I noticed some features of the TPL that I hadn’t thought about before. [And also noticed some features mentioned that didn’t make it through to the release version of the library such as Task.Current where there is now only an internal InternalCurrent, and mention of a Blocking status in Task.Status]

Tasks that are created inside other tasks are by default independent, but there are configuration options to allow you to attach the children to the parent, so that the parent doesn’t finish until all fo the children have completed.

var task = Task.Factory.StartNew(delegate {
var nestedTask = new Task(delegate
Console.WriteLine(“Finishing 2”);
}, TaskCreationOptions.AttachedToParent);
Console.WriteLine(“Finishing 1”);

Console.WriteLine(“Finished main”);

There is also the .NET version of a promise, a TaskCompletionSource. An instance of this type has a Task property that returns a Task, and the state of this task can be set using methods on the TaskCompletionSource instance. For example, you can set a result, an exception and also set the task as cancelled using the Try… methods.

var tcs = new TaskCompletionSource<int>();


Console.WriteLine(“Finished main”);

Duffy’s book brings together and explains a mass of interesting concurrency observations and is well worth many reads.

This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s