Sometime ago a good friend of mine asked me about the differences between auto-implemented properties and standard properties but due we were in a rush it was no possible to make some time for explaining.

Simply put, the compiler creates the internal object and the accessors (get and set operations) for the auto-implemented properties so they’re mutable and after declaration the client code can change the values. This is an example of auto-implemented properties:

    public class Population
    {
        public int Male { get; set; }
        public int Female { get; set; }
    }

By other hand, in properties the internal object and the accessors are up to you, so you need to write the necessary code as shown in the following example:

    public class Population
    {
        public int male;
        public int Male
        {
            get
            {
                return male;
            }
            set
            {
                male = value;
            }
        }

        public int female;
        public int Female
        {
            get
            {
                return female;
            }
            set
            {
                female = value;
            }
        }
    }

Until this point, properties and auto-implemented properties seem identical in practical terms and in fact they’re in simple scenarios like previous but in complex classes that contain methods as well as significant logic, properties are the best option because you will be able to customize depending on your needs. Imagine you need a simple operation to get one of the values as follows:

    public class Population
    {
        public int Total { get; set; }
        public int Male { get; set; }
        public int Female {
            get
            {
                return Total - Male;
            }
        }
    }

In the previous example, the Female field is calculated, so it needs some logic and thus a property is better than an auto-implemented property. You can use the auto-implemented properties for simple, small Models and properties for more complex entities. According to my experience, only a 20% of your classes do the job, so it’s probably that 20% of your classes be suitable to be Models with auto-implemented properties.

There are some links you can use to read further about Auto-Implemented Properties and Properties. Finally there is a C# oddity related to how declaring read-only auto implemented properties, would you use it?

public int Female { get; private set; }

Greetings,

Javier Andrés Cáceres Alvis

Microsoft Most Valuable Professional – MVP
Intel Black Belt Software Developer