Galin Iliev's blog

Software Architecture & Development

C# 3.0 Auto-Implemented Properties

I have downloaded VS Orcas March CTP and I am playing with its features now.

I found one very cool feature called Auto-Implemented Properties. This will allow you to create private field and public property accessor with single C# 3.0 line like this:

public string FullName { get; set; }

instead like in c# 2.0:

private string fullName;

 

public string FullName

{

    get { return fullName; }

    set { fullName = value; }

}

And as with the others cool language sugars the compiler will do the rest :). Actually it doesn't look that good but still it works. For the single line property above I got the following code in Reflector:

public string FullName

{

    [CompilerGenerated]

    get

    {

        return this.<>k__AutomaticallyGeneratedPropertyField0;

    }

    [CompilerGenerated]

    set

    {

        this.<>k__AutomaticallyGeneratedPropertyField0 = value;

    }

}

Nice huh?

Of course this won't work if you have complex getters and setters but still will speed up code writing and leave more time for thinking :)

Loading