Blog - Title

October, 2007

• Covariance and Contravariance in C#, Part One

I have been wanting for a long time to do a series of articles about covariance and contravariance (which I will shorten to “variance” for the rest of this series.) I’ll start by defining some terms, then describe what variance features C# 2.0 and...
• Covariance and Contravariance in C#, Part Two: Array Covariance

C# implements variance in two ways. Today, the broken way. Ever since C# 1.0, arrays where the element type is a reference type are covariant . This is perfectly legal: Animal[] animals = new Giraffe[10]; Since Giraffe is smaller than Animal...
• Path Finding Using A* in C# 3.0, Part Two

In order to implement the A* algorithm in C# 3.0 I am going to need to implement some custom data structures. Today we’ll consider how to implement the “path”. You’ll notice in my description of the algorithm that the only operations we perform on...
• Covariance and Contravariance in C#, Part Three: Method Group Conversion Variance

Last time I discussed how array covariance is broken in C# (and Java, and a number of other languages as well.) Today, a non-broken kind of variance supported by C# 2.0: conversions from method groups to delegates. This is a more complicated kind of variance...
• Path Finding Using A* in C# 3.0, Part One

As we get into the home stretch for the Orcas release I’ve been spending a little time lately just playing around implementing some “classic” algorithms and data structures in C#. The one I implemented today is the famous A* algorithm. Surely you’ve...
• Path Finding Using A* in C# 3.0, Part Four

Finally we are ready to translate our pseudocode into C# 3.0. What do we need to make the algorithm run? We need a start node, a destination node, a function which tells us the exact distance between two neighbours, and a function which tells us the...
• Covariance and Contravariance in C#, Part Eight: Syntax Options

As I discussed last time, were we to introduce interface and delegate variance in a hypothetical future version of C# we would need a syntax for it. Here are some possibilities that immediately come to mind. Option 1: interface IFoo<+T, -U>...
• Covariance and Contravariance In C#, Part Five: Higher Order Functions Hurt My Brain

Last time I discussed how we could in a hypothetical future version of C# allow delegate types to be covariant in their return type and contravariant in their formal parameter types. For example, we could have a contravariant action delegate: delegate...
• Path Finding Using A* in C# 3.0, Part Three

In order to make the A* algorithm work we need to get the lowest-estimated-cost-path-discovered-so-far out of the list of paths under consideration. The standard data structure for doing so is called a “priority queue”. Priority queues are so-called because...
• Covariance and Contravariance in C#, Part Four: Real Delegate Variance

In the last two posts I discussed the two kinds of variance that C# already has -- array covariance and member-group-to-delegate conversion covariance (on return types) and contravariance (on formal parameter types). Today I want to generalize the...