Automatic Properties Work in .NET 2.0

This was sort of an accidental discovery, I'm not the first to stumble on it, but it doesn't seem to be widely known. A colleague of mine asked me about an interesting behavior he noticed after switching to Visual Studio 2008.  He was still targeting the .NET 2.0 framework, but noticed the automatic property feature worked.

Automatic properties are where you can just say

public string SomeProperty { get; set; }

The compiler provides the backing field and implements the get and set for you. It's a very nice shorthand syntax for a common pattern. We were all under the impression that it was a new C# 3.0 language feature and could only be used if you targeted version 3.5 of the .NET Framework. After searching around for why it was still working on a .NET 2.0 app, I found many who verified what we were seeing. Indeed it does work, because it's a compiler feature, and 2008 uses the latest compiler even if you are targeting 2.0. Cool.

2 Comments

  • I suppose this makes sense since .NET 3.0 and 3.5 both run on the "2.0" framework and it wouldn't make sense to have 3 branches of .NET 2.0 runtime compilers.

    However I wonder how many of the .NET 3.5 features are just compiler tricks.

  • Oh Now I understand.
    I thought it was a problem and I have posted for this on microsoft forum in April
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3245715&SiteID=1
    Thanks Loren for Clarification

Comments have been disabled for this content.