Using C# 3.0 from .NET 2.0

Yesterday I wrote an article on the property snippet in Visual Studio 2008 and that it had changed from Visual Studio 2005.Only a few hours later, I questioned myself if even a few people might be interested in this. Not because of the snippet, but because of the abilities that Visual Studio 2008 and its C# compiler bring you.

Note : I do have interest for it, because I still have to teach people .NET 2.0.They look ‘gobsmacked’ at the automatic properties and as I don’t want to confuse them, I still like to have the old property snippet! 🙂

Daniel Moth reminded me of an article of him from back in may, where he wrote about using C# 3.0 features in .NET 2.0 and the confusion in discussions about this ‘feature’.

The truth is, some of the new C# 3.0 features are purely compiler magic. So when you’re using Visual Studio 2008 and are working on a .NET 2.0 project, you can use a selected set new features. Not because they’re C# 3.0 features, but because they’re features of the new compiler!

  • Local variable inference
  • Object initializers
  • Anonymous types
  • Lambda expressions

Daniel also explains that other features don’t work, because they rely on the System.Core.dll assembly. If you want to use extension methods for example, that’s not possible. You’re missing the ExtensionAttribute. Copy the following code into a Visual Studio 2008 .NET 2.0 project.

public static class Extensions {   ///   /// Example of an extension method in C# 2.0   ///   /// This disappears   /// The character to look for in the string   /// The number of times the specified character exists in the string.   public static int CountChars(this string s, char value)   {     int startIndex = 0, foundAt = 0, timesCounted = 0;     while ((foundAt = s.IndexOf(value, startIndex)) != -1)     {       startIndex = foundAt + 1;       timesCounted++;     }     return timesCounted;   } }

Now you get a nice error…

extensionmethoderror

Daniel again explains that this is really easy by adding that attribute ourselves. Check his C# 3.0 features in .NET 2.0 article for it. I’ve created a full solution though, which you can download at the bottom.

But Daniel goes even further by explaining how you can make LINQ work in .NET 2.0.Don’t forget that LINQ isn’t ‘just’ all compiler magic. The query expressions (using ‘from’, ‘where’, ‘select’, etc. (stand-alone) keywords) are, but the operators aren’t. Daniel explains the differences over here. Of course all the implementations of LINQ to objects, LINQ to SQL, LINQ to XML, etc live in the .NET Framework 3.5 assemblies. But what if we were to write our own? Again, there’s an example for this and you can guess from who. Find it here.

After implementing that class, you can easily write the following code in your .NET 2.0 project.

var ints = new[] { 1, 2, 3, 4 }; var res2 = from p in ints           where p > 2           select p;

Now isn’t that just cool?

Remember though, if you’re on a .NET 2.0 project and the other developers don’t have Visual Studio 2008 installed, you’re in for a problem. This doesn’t work in Visual Studio 2005!

Download the solution with all the code here.