C# 3.0 : Automatic Properties

February 24, 2008

Hello ,

Being a C# programmer , you’ve used to leverage propcode snippet to create a property . C# 3.0 provides you with a nice feature called “Automatic Properties” which enables you to avoid having to declare private accessor field for properties and write get/set logic , this will make your code more concise and legible . However , you can still create regular properties and change get/set logic.

This is a property created by C# 2.0 :

        private string propertyName;
        public string PropertyName
        {
            get { return propertyName; }
            set { age= propertyName; }
        }

And this one is re-writed using C# 3.0 :

public string PropertyName { get; set; }

Implementing this feature , the compiler automatically creates the get/set logics . If you need to make the above property readonly , add private keyword before set accessor :

public string PropertyName { get; private set; }