Welcome to MSDN Blogs Sign in | Join | Help

Cool new C# syntactic sugar

Just the other day I ranted about all the explicit get; set; implementation and I found this out today.

I was reviewing some code and I saw something like this

class MyClass
{
    public int Property {
        get; set;
    }
}

class Program
{
    static void Main(string[] args)
    {
        MyClass mc = new MyClass();
        mc.Property = 42;
        Console.WriteLine(mc.Property);
    }
}

I was completely confused. How the hell is there a abstract property in a concrete class? Then I figured out that this is new short-hand supported by Visual Studio 2008 (code-named Orcas and you can check this out in the latest beta).

The compiler generates the field for the property and also generates the code in the get set to point to the field.

internal class MyClass
{
    // Fields
    [CompilerGenerated]
    private int <Property>k__BackingField;

    // Properties
    public int Property
    {
        [CompilerGenerated]
        get
        {
            return this.<Property>k__BackingField;
        }
        [CompilerGenerated]
        set
        {
            this.<Property>k__BackingField = value;
        }
    }
}
Published Friday, September 21, 2007 10:42 AM by abhinaba
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Cool new C# syntactic sugar

Friday, September 21, 2007 1:54 AM by DanielMoth

FYI, the "syntactic sugar" feature has a name :-)

Automatic Properties

(aka auto-implemented properties)

# re: Cool new C# syntactic sugar

Friday, September 21, 2007 3:08 AM by Thierry

What is the use of this feature, except perhaps the ability to set a breakpoint?

# re: Cool new C# syntactic sugar

Friday, September 21, 2007 4:05 AM by abhinaba

FxCop guideline is to wrap publicly visible fields in properties. Hence you land up writing a lot of these call only Properties. The Automatic Property will help in that.

Remember you cannot change a field to a Property later because all assemblies referring to it will have to be recompiled (see http://blogs.msdn.com/abhinaba/archive/2006/04/11/572694.aspx) .

# re: Cool new C# syntactic sugar

Friday, September 21, 2007 10:52 AM by Nicholas Paldino [.NET/C# MVP]

It should be noted that if you are going to serialize any of these classes, then it is a bad idea to use the auto implemented properties.

# re: Cool new C# syntactic sugar

Friday, September 21, 2007 11:23 AM by Raj Kaimal

Note that you can also do this:

public int Property {

       get; private set;

   }

# re: Cool new C# syntactic sugar

Friday, September 21, 2007 1:13 PM by DanielMoth

Thierry, in addition to the versioning issue that Abhinaba points out, fields aren't always bindable whereas properties are.

# re: Cool new C# syntactic sugar

Sunday, September 23, 2007 9:00 AM by William Kempf

I've blogged about this being the "Lamest New Feature in C# 3.0".  http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!234.entry

It really is a waste of syntactic sugar.

# re: Cool new C# syntactic sugar

Monday, September 24, 2007 7:19 AM by Rujith

Well I would say as a programer, I hate this feature.

# re: Cool new C# syntactic sugar

Thursday, September 27, 2007 9:50 AM by John

I have to agree with Rujith. To me it just seems like a lazy feature for a lazy programmer.

# re: Cool new C# syntactic sugar

Tuesday, November 13, 2007 1:49 PM by Marv

the less code, the less errors, good thinking Microsoft...

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker