DCSIMG
C#3.0 Feature: Auto-Implemented Properties, Nothing More Than A Compiler Trick - Yochay Kiriaty

Yochay Kiriaty

I find your lack of faith disturbing

C#3.0 Feature: Auto-Implemented Properties, Nothing More Than A Compiler Trick

During my C# 3.0 deep dive session in the Developer Academy II, I presented a new syntactic sugar feature in C# 3.0 called Auto Implemented Properties. The Auto Implemented Properties allow us to take regular properties fields that looks like this

//Simple C# 2.0 (.NET Framework 2.0) property decleration

    private string customerID;

    public string CustomerID

    {

        get { return customerID; }

        set { customerID = value; }

    }

 

And simply write this one line of code:

// C#3.0 Feature:  Auto-Implemented Properties

public string CustomerID { set; get; }

The compiler, behind the scenes creates the supporting backing field named CustomerID and also creates the default set & get supporting methods. Using Reflector to check the compiler output you will see this:

clip_image002

 

Note the default setters and getters generated by the compiler and a default constructor.

 

 clip_image004

 

If you want to modify the values in the SET function operation or make format manipulation at the GET function operation you will need to use the old syntax.

So you can see that this only a compiler feature, a trick, no magic nor new code, it is just shorter code that is easy to read and maintain.

Published Sunday, December 09, 2007 9:33 AM by Mr.J

Comments

# re: C#3.0 Feature: Auto-Implemented Properties, Nothing More Than A Compiler Trick@ Sunday, December 09, 2007 10:44 AM

This is by far the least meaningful and/or useful features in C# 3.0...

# re: C#3.0 Feature: Auto-Implemented Properties, Nothing More Than A Compiler Trick@ Sunday, December 09, 2007 5:26 PM

Even so it is a setup for the creation of an Anonymous Ttypes. Therefore I need to cover it while going over some key features in C#3.0 in future blogs. Like tomorrow . . .

by Mr.J

# re: C#3.0 Feature: Auto-Implemented Properties, Nothing More Than A Compiler Trick@ Sunday, December 09, 2007 8:39 PM

Just expressing my dissatisfaction with the feature, not your series :)

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: