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


{


  /// <summary>


  /// Example of an extension method in C# 2.0


  /// </summary>


  /// <param name=”s”>This disappears</param>


  /// <param name=”value”>The character to look for in the string</param>


  /// <returns>The number of times the specified character exists in the string.</returns>


  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.

You may also like...

8 Responses

  1. Hmmm, I could probably get rid of either the foundAt or startIndex variable.

  2. Mark says:

    Why not just compile against .NET 3.0? It’s backwards compatible, right?

  3. They’re not (just) backwards compatible, they are exactly the same. It’s a single runtime for .NET Framework 2.0 and upwards. Again, in .NET Framework 3.5, you’re still coding against the .NET 2.0 Runtime!!!

    In .NET Framework 3.5 however, you get a much smarter compiler and a lot of library classes.

    Just start a new project and choose Console Application in .NET Framework 3.5 !!! The project template automatically adds System.Core assembly. Remove it and do a single Console.WriteLine(“Hello world from a .NET 3.5 application.”);

    Now compile and copy to a computer with .NET 2.0, but without .NET Framework 3.5

  4. Mark says:

    I do understand that the difference between 3.5 and 2.0 is just the class library and the compiler, thats what makes it “backwards compatible”.

    But still, my question remains, why should you go through all the trouble of creating your own ExtensionAttribute, LINQ classes, etc? When all this is available in .NET 3.5?

  5. Because almost no one is building using .NET Framework 3.5 yet? Still a large amount of our customers are working in .NET 1.1 !!! Of course not anymore after we’ve left πŸ˜‰

  6. hfrmobile says:

    I found a MSDN blog entry discussing “using C# 3.0 with Visual Studio 2005” (200five not 8eight). Is this possible?

    See:
    http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/fd8cfaf1-05e3-4bd8-a663-65e0f4981cd3

  7. Dennis van der Stelt says:

    @hfrmobile : Not that I know of. The thing is that those new features are baked into the compiler. And I’ve never seen how you can replace the compiler in a Visual Studio version, especially knowing that the compiler is also used in the background for intellisense and a lot of other stuff.

  8. hfrmobile says:

    What about:

    (not tried this …)

Click on a tab to select how you'd like to leave your comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.