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.
- 1.0 - released in 2002
- 1.1 - released in 2003
- 2.0 - released in 2005, with a new CLR (to handle generics and nullable types) and compilers for C# 2 and VB 8.
- 3.0 - released in 2006, this is just 2.0 plus new libraries: Windows Presentation Foundation, Windows Communication Foundation, Workflow Foundation, and Cardspace
-
3.5 - released in 2007, this is 3.0 plus new libraries (primarily LINQ and some extra "base" libraries such as
TimeZoneInfo
) and new compilers (for C# 3 and VB 9) - 4 - released in 2010, this includes a new CLR (v4), new libraries, and the DLR (Dynamic Language Runtime)
- 4.5 - released in 2012, this allows for WinRT development on Windows 8 as well as extra libraries - with much wider async APIs
C# language versions
There are five significant language versions:
- C# 1
- C# 2, introducing generics, nullable types, anonymous methods, iterator blocks and some other more minor features
- C# 3, introducing implicit typing, object and collection initializers, anonymous types, automatic properties, lambda expressions, extension methods, query expressions and some other minor features
- C# 4, introducing dynamic typing, optional parameters, named arguments, and generic variance
- C# 5, introducing asynchronous functions, caller info attributes, and a tweak to
foreach
iteration variable capture
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:
- VS.NET 2002 - support for C# 1 and .NET 1.0
- VS.NET 2003 - support for C# 1 and .NET 1.1
- VS 2005 - support for C# 2 and .NET 2.0, and .NET 3.0 with an extension
- VS 2008 - support for C# 3 and .NET 2.0, 3.0 and 3.5 (multi-targeting)
- VS 2010 - support for C# 4 and .NET 2.0, 3.0, 3.5 and 4
- VS 2012 - support for C# 5 and .NET 2.0 to 4.5 (including WinRT on Windows 8), and portable class libraries
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...)
- You can't use C# 2 features without at least VS 2005
- You can't use C# 3 features without VS 2008
- You can't ask VS 2005 or VS 2008 to target .NET 1.0 or 1.1 (there's an extension for it, but I haven't used it - expect some pain for debugging etc)
- You can't force VS 2008 to restrict you to only C# 2 features, or force VS 2005 to restrict you to C# 1 features
- Each version of Visual Studio has its own project file format and will upgrade your older projects when you first load them in that version. (The differences between VS 2003 and VS 2005 were significant; the differences between VS 2005 and VS 2008 are much smaller.)
- VS 2008 has special support (in project properties) for which framework version you want to target: 2.0, 3.0 or 3.5
- You can use most C# 3 features when targeting .NET 2.0 or 3.0, but not quite all
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);
if (x < 10) { Console.WRiteLine("Yes"); }
(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.