Returning from Copenhagen

I’m currently sitting in Copenhagen airport, with two hours to wait before my flight boards. It’s been a tiring day – my feet in particular are aching somewhat, and my throat is quite sort – but I’ve thoroughly enjoyed it. We started the actual talk at about 10am and finished at 4pm, with three breaks totalling about an hour – so I was speaking for five hours. You might have thought that with all that time, it would be easy to go into a lot of detail about C# 2 and 3, but there’s really too much to cover everything. … Continue reading Returning from Copenhagen

C# 4.0: dynamic<T> ?

I’ve not played with the VS2010 CTP much yet, and I’ve only looked briefly at the documentation and blogs about the new C# 4.0 dynamic type, but a thought occurred to me: why not have the option of making it generic as a way of saying “I will dynamically support this set of operations”? As an example of what I mean, suppose you have an interface IMessageRouter like this: public interface IMessageRouter{    void Send(string message, string destination);} (This is an arbitrary example, by the way. The idea isn’t specifically more suitable for message routing than anything else.) I may have various … Continue reading C# 4.0: dynamic<T> ?

What other Enumerable extension methods would you like to see?

A few questions on Stack Overflow have suggested to me that there might be some bits missing from LINQ to Objects. There’s the idea of a “zip” operator, which pairs up two sequences, for instance. Or the ability to apply the set operators with projections, e.g. DistinctBy, IntersectBy etc (mirroring OrderBy). They’re easy to implement, but it would be nice to get a list of what people would like to see. So, what’s missing?

DeveloperDeveloperDeveloper: Registration now open (hurry!)

The registration page for DeveloperDeveloperDeveloper Day 2008 (Reading, November 22nd) is now open. In the past this has been heavily oversubscribed, so if you want to come you’ll need to register quickly. I’ve got a speaking slot in the afternoon: implementing LINQ to Objects in 60 minutes. As always, I’m very much looking forward to it.

Mapping from a type to an instance of that type

A question came up on Stack Overflow yesterday which I’ve had to deal with myself before now. There are times when it’s helpful to have one value per type, and that value should be an instance of that type. To express it in pseudo-code, you want an IDictionary<typeof(T), T> except with T varying across all possible types. Indeed, this came up in Protocol Buffers at least once, I believe. .NET generics don’t have any way of expressing this, and you end up with boxing and a cast. I decided to encapsulate this (in MiscUtil of course, although it’s not in … Continue reading Mapping from a type to an instance of that type

Why boxing doesn’t keep me awake at nights

I’m currently reading the (generally excellent) CLR via C#, and I’ve recently hit the section on boxing. Why is it that authors feel they have to scaremonger about the effects boxing can have on performance? Here’s a piece of code from the book: using System; public sealed class Program {   public static void Main() {      Int32 v = 5;   // Create an unboxed value type variable. #if INEFFICIENT      // When compiling the following line, v is boxed      // three times, wasting time and memory      Console.WriteLine(“{0}, {1}, {2}”, v, v, v);#else      // The lines below have the same result, execute      // much faster, and use … Continue reading Why boxing doesn’t keep me awake at nights

DotNetRocks interview

Last Monday evening I had a chat with the guys from DotNetRocks, and today the show has gone live. I wouldn’t claim to have said anything particularly earth-shattering, and regular readers will probably be familiar with many of the themes anyway, but I thoroughly enjoyed it and hope you will too. Amongst other things, we talked about: Protocol buffers Implicit typing and anonymous types Why it doesn’t bother me that Office hasn’t been ported to .NET C# 4 My wishlist for C# Threading and Parallel Extensions Working for Google How to learn LINQ C# in Depth Feedback welcome. And yes, … Continue reading DotNetRocks interview

Non-nullable reference types

I suspect this has been done several times before, but on my way home this evening I considered what would be needed for the reverse of nullable value types – non-nullable reference types. I’ve had a play with an initial implementation in MiscUtil (not in the currently released version) and it basically boils down (after removing comments, equality etc) to this: public struct NonNullable<T> where T : class{    private readonly T value;     public NonNullable(T value)    {        if (value == null)        {            throw new ArgumentNullException(“value”);        }        this.value = value;    }     public T Value    {        get        {            if (value == null)            {                throw new … Continue reading Non-nullable reference types

Formatting strings

A while ago I wrote an article about StringBuilder and a reader mailed me to ask about the efficiency of using String.Format instead. This reminded me of a bone I have to pick with the BCL. Whenever we make a call to String.Format, it has to parse the format string. That doesn’t sound too bad, but string formatting can be used a heck of a lot – and the format is almost always hard-coded in some way. It may be loaded from a resource file instead of being embedded directly in the source code, but it’s not going to change … Continue reading Formatting strings