Automatic Implemented Properties in C#

Auto-implemented properties were introduced in C# 3.0 to make developers lives simpler by avoiding default implementation of properties. 

Let's say, we create a public property called Model. The default implementation of the property looks like Listing 1. 

public partial class Car
{
    private string model; 

    public string Model
    {
        get { return model; }
        set { model = value; }
    }
}

Listing 1

The same code can be replaced with Listing 2. 

 public string Model { get; set; } 

Listing 2

Listing 2 is automatic implemented property example. You must implement both getter and setter on a property. The auto-implemented properties code basically tell compiler that this property is default property implementation and compiler writes code that is needed to do exactly same what Listing 1 does.

So you would think what do you need the automatic properties? Well, they make your code look cleaner and faster.

If you want to learn more about properties in C#, here are couple of great articles:



Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.