Use C# 3 features from C# 2 and .NET 2.0 code (var keyword, anoynoymous types, auto-properties and more from .NET 2.0 project)

clip_image002Visual Studio 2008 and .NET 3.5 are already here, but some of us, sometimes, still need to use .NET 2.0.
Visual Studio 2008, support in a new feature called “Multi Targeting”. You can use Visual Studio 2008 ad IDE for .NET 2.0 and .NET 3.0 too. Simply, in the “New Project” form, choose the version you want.

The common between all this versions you can use VS2008 to develop for it, is that this entire versions are actually based on the same version of CLR – CLR 2.0.

.NET 3.5 based in the same version of CLR that .NET 2.0 uses. Of course, there are a lot of new features, new C# and VB.NET versions, but under the covers the same CLR works.

Why is it so important? Because theoretically, you can use some of the mew features of .NET 3.5 in .NET 2.0 when you work from VS 2008.

In this post, I'll show you some of the new C# 3 and .NET 3.5 features and how they work in .NET 2.0 project (that created and developed from Visual Studio 2008). For every feature I'll give you a look of which code is actually generated. I give you the original code, and the disassembly code from Reflector.

All the code in this post written in Visual Studio 2008 in a .NET 2.0 Console Application projects, without reference to any addition assemblies, and checked in machine without .NET 3.5.

one of the new features in C# 3 is auto-properties. because, actually, a lot of the properties we write are very simple, we don't have to write the full code (declare private variable, create getter and setter). We just have to write this:

public string Name { get; set; }

The same code of auto-properties works from .NET 2.0 projects under Visual Studio 2008. That's because the auto-properties is only a compiler trick.  Let's see how this code looks in reflector (in .NET 2.0 project, same like in .NET 3.0 project):

   1:      // Fields
   2:      [CompilerGenerated]
   3:      private string <Name>k__BackingField;
   5:      // Properties
   6:      public string Name
   7:      {
   8:          [CompilerGenerated]
   9:          get
  10:          {
  11:              return this.<Name>k__BackingField;
  12:          }
  13:          [CompilerGenerated]
  14:          set
  15:          {
  16:              this.<Name>k__BackingField = value;
  17:          }
  18:      }
  19:  }

As you can see, what's actually happened, is that the compile create new private variable, and public property with simple getters and setters. because that's actually a compiler trick, we can use it in .NET 2.0 projects too.

Object Initializers

   1:  List<Book> books = new List<Book>();
   2:  books.Add(new Book() { Name = "Enders Game", ISDN = "13456789" });

This simple code is an example of the new Object Initializers in C# 3. But, this code works when you create a .NET 2.0 project too. Because, again, it's simple compiler trick. This is the output of the Reflector's disassemble for this code (in .NET 2.0 project under Visual Studio 2008):

   1:  internal class Program
   2:  {
   3:      // Methods
   4:      private static void Main(string[] args)
   5:      {
   6:          List<Book> books = new List<Book>();
   7:          Book <>g__initLocal0 = new Book();
   8:          <>g__initLocal0.Name = "Enders Game";
   9:          <>g__initLocal0.ISDN = "13456789";
  10:          books.Add(<>g__initLocal0);
  11:      }
  12:  }

Behind the scenes, this code creates a new instance of the Book object, and simply give values to the properties. Because .NET 2.0 and .NET 3.5 are actually working on the same CLR, the same code generated, and it's working on .NET 2.0 too.

"var" keyword

the keyword "var" is new in C# 3, and give us the option to declare a new variable without specify the type. the type will be the type of the value that we will put to the variable:

var i =5;

this code will work in .NET 2.0 project in Visual Studio 2008 too. because again, it's compiler code. If I'll check the Reflection's disassembly, I'll see:




the compiler is smart. when i only declare a variable but never use it, it will not compile. but, if I'll change a little the code, and print this variable:

   1:  var i = 5;
   2:  Console.WriteLine(i);

then, we'll see this:

image  We can use this feature in .NET 2.0 project, because it's only a compiler trick. actually, "var" only tell the compiler to replace it with the type name of the value we use. in this example, we see that int replaced the var keyword.

More than that. we also can use Anonymous Type in .NET 2.0 project:

   1:  var i = new { Name = "Enders Game", ISDN = "123456789" };
   2:  Console.WriteLine(i.Name);

The disassembly:


WOW! What is this "var" keyword in the disassembly? this is .NET 2.0 project, and in C# 2 there is no "var" keyword.
Actually, a new class was generated:


But, this class is hidden with DebuggerHiddenAttribute and DebuggerDisplayAttribute so, in the debugging we can't see it.

And again, we see how we can use .NET 3.5 feature and C# 3 keyword in .NET 2.0 project under Visual Studio 2008. It's possible just because this (and everything else we see in this post) is a compiler trick, which doesn't requied any additional assemblies or features - and use CLR v2.0.

Extension Methods
Daniel Moth wrote about using Extension Methods in .NET 2.0 here.


Visual Studio 2008 support Multi Targeting which give us the option to develop .NET 2.0 project in VS 2008 and compile it with new version of the compiler. this compiler create a regular .NET 2.0 code - but give us the option to use .NET 3.5 and C# 3 features, that are actually compiler-tricks to work easily.
Behind the scenes, the compiler generate regular .NET 2.0 code.



Comments have been disabled for this content.