C# in Depth

Cover of C# in Depth
Order now (3rd edition)

Untangling the Versions

There are lots of different versions of different elements of development. You need to distinguish between the versions of Visual Studio (the IDE), C# (the language) and .NET (the framework). It's quite hard to talk about each of these individually without bringing in other pieces, but I'll see what I can do... Note that I'll avoid introducing the CLR versions as well, as most developers don't really need to know about that.

.NET Framework versions

There have been seven significant releases of the .NET Framework, excluding service packs. The framework includes the compilers, runtime, and libraries. Additionally, there are other profiles such Silverlight which complicate matters.

C# language versions

There are five significant language versions:

See the specifications page for downloads for each version, from Microsoft and ECMA.

Visual Studio versions

For a long time, releases of Visual Studio were closely tied to framework releases. The picture has become a bit more flexible and complicated, however:

That's all the theory. Here are the practical limitations and working configurations. Note that this assumes you want to use Visual Studio - if you're happy to use just the command line compiler, that's a slightly different story which I'll avoid for simplicity's sake. (At some point I'll return to this page to talk about C# 4 and C# 5 features, but not just now...)

Using C# 3 in .NET 2.0 and 3.0

Some C# 3 features can be used freely in .NET 2.0; some require a bit of extra work; one doesn't work at all:

Fully available features

Automatically implemented properties, implicitly typed local variables and arrays, object and collection initializers, anonymous types, partial methods, and lambda expressions can all be used at will. Note that lamdba expressions are slightly less useful in .NET 2.0 without the Func<...> and Action<...> families of delegate types, but these can easily be declared in your own code:

public delegate void Action();
public delegate void Action<T1, T2>(T1 arg1, T2 arg2);
public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);
public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
public delegate TResult Func<TResult>();
public delegate TResult Func<T, TResult>(T arg);
public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);

(Note that Action<T> is part of .NET 2.0, hence its absence above.)

Partially available features - extension methods and query expressions

Extension methods require an attribute which is normally part of .NET 3.5. However, you can define it yourself, at which point you can write and use extension methods to your heart's delight:

namespace System.Runtime.CompilerServices
{
    [AttributeUsageAttribute(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
    public class ExtensionAttribute : Attribute
    {
    }
}

Query expressions themselves are available regardless of framework version, as they're only translations into "normal" C# 3 - however, they're not much good unless you've got something to implement the Select, Where etc methods. These are normally part of .NET 3.5, but LINQBridge is an implementation of LINQ to Objects for .NET 2.0. This allows in-process querying with query expressions.

Unavailable feature - expression trees

As far as I'm aware, there's no way to get the compiler to create expression trees when using .NET 2.0, not least because all the expression tree library classes are part of .NET 3.5. It's just possible that there may be a way to reimplement them just as LINQBridge reimplements LINQ to Objects, but I wouldn't hold your breath - and it would be much more complicated to do this. As a corollary, you can't use "out of process" LINQ without .NET 3.5, as that relies on expression trees.

Hope all this makes things a bit clearer! Please mail me if you have any further questions on the topic.