Blog LogoBlog Logo

Automatic Properties

18 Jun 2007 ~ 4 min read


Continuing my look at the new features found in the C# 3.0 compiler I will look at Automatic Properties.

public class Employee
{
    public string FirstName { get; set; }
    public string Surname { get; set; }
    public DateTime DateOfBirth { get; set; }
    public Employee Manager { get; set; }
}

At first look this might seem more like a definition for an interface, but for the class keyword and the member visibility modifiers. However, this is actually a new feature where by the compiler will automatically fill in the member fields. This is useful for situations where nothing needs to be done when getting or setting values from the member fields. It means they can be constructed much more quickly as there is no need for tedious creating of private member fields then the public properties. All that is needed is one simple construct. It is also possible to reduce the visibility of the getter and setter independently. For example, in the above example you may wish to make DateOfBirth to be effectively read-only for all by the class that owns it. You can prefix the set keyword with the private visibility modifier. But what is the compiler actually producing? The following is the output from Lutz Roeder’s Reflector for the DateOfBirth property:

[CompilerGenerated]
private DateTime <>k__AutomaticallyGeneratedPropertyField2;

public DateTime DateOfBirth
{
    [CompilerGenerated]
    get
    {
        return this.<>k__AutomaticallyGeneratedPropertyField2;
    }
    private [CompilerGenerated]
    set
    {
        this.<>k__AutomaticallyGeneratedPropertyField2 = value;
    }
}

In the code, only the  property is accessible. Attempting to use the compiler generated name produces a compiler error. If the full name (as shown above) is used then the compiler will complain with three errors:

#ErrorFileRowColProject
1Identifier expectedConsoleApplication2Employee.cs2018ConsoleApplication2
2Invalid expression term ’>’ConsoleApplication2Employee.cs2019ConsoleApplication2
3; expectedConsoleApplication2Employee.cs2020ConsoleApplication2

Compiler errors {border=“1” cellspacing=“0” cellpadding=“1” width=“100%”}

If the <> are removed the message changes to:

#ErrorFileRowColProject
1‘ConsoleApplication2.Employee’ does not contain a definition for ‘k__AutomaticallyGeneratedPropertyField2’ and no extension method ‘k__AutomaticallyGeneratedPropertyField2’ accepting a first argument of type ‘ConsoleApplication2.Employee’ could be found (are you missing a using directive or an assembly reference?)ConsoleApplication2Employee.cs2018ConsoleApplication2

So, why go to all this trouble? Surely it isn’t just to save a few key strokes?

Part of the answer can be seen in a post I made in November 2005: Why make fields in a class private, why not just make them public? and there was a follow up in June 2006 when I returned to The Public Fields Debate Again.

In short, public fields and public properties, although they appear to look identical to the outside in C# are sytactically different once compiled. Automatic Properties are a way to address that. If you, at some point in the future, decide that you need the property to do more then the external interface of the object won’t change, you just turn the automatic property into a normal property/field combination again.

NOTE: This post was rescued from the Google Cache. The original date was Tuesday, 13th March, 2007.

🔄 Follow up - 9th August 2026

When this post was originally created Reflector was free, since then RedGate bought it and now charge for it. If you want an equivalent free product that does the same thing then have a look at dotPeek from JetBrains.


Headshot of Colin Mackay

Hi, I'm Colin. I'm a software engineer with over 30 years of experience based in central Scotland, specialising in Microsoft technologies, initially with C++ and then in C#. I was a Microsoft MVP from 2007 to 2010 and a Code Project MVP from 2005 to 2009.