Over a million developers have joined DZone.

Exploring Auto-Implemented Properties of C# 3.0

DZone's Guide to

Exploring Auto-Implemented Properties of C# 3.0

· ·
Free Resource

Note: This is the first part of my C# 3.0 tutorial series.

Auto-implemented properties is the C# 3.0 new feature that make property-declaration more concise. It helps you to save some of your time for typing a lot of codes. Please take a look the following code to know how it looks like.

class Car
public string Speed { get; set; }

Why Auto-Implemented Properties?

Looking back the way that we have been doing for creating a property in so many projects, do you notice that you have been writing so many line codes just for creating a simple property? Look at the code below.

public class Employee {
private int _id;
private string _firstName;
private string _lastName;

public int ID{
return _id;

public string FirstName {
get {
return _firstName;
set {
_firstName = value;

public string LastName {
get {
return _lastName;
set {
_lastName = value;

Code 1.0: Employee class that doesn't use auto-implemented properties

This is what we used to create the property in C#. In order to create one simple property, we have to type extra four or five lines of code. So, if you have one hundred properties in different entity classes, you have to type toooo much code just for creating properties.

I think VS IDE team at Microsoft also awared of this situration. That's why when they released Visual Studio 2005 with .NET framework 2.0, we got the better way to deal with creating properties. You can simply type "prop" and press "tab" key twice. then, VS IDE will generate the property for you. Or you can use "Encapsulate Field" from "Refactor" menu to create the property. Unfortunately, those features are available only in C# 2.0.

And when Microsoft shipped .NET framework 3.0, the new feature called "Auto-Implemented Properties (a.k.a Automatic Properties)" is introduced in Framework 3. It helps you to save some of your typing time for declaring private variable, setting the value to that private variable and returning the value.

Take a look at the class that I have converted from normal class (Code 1.0) to the class with auto-implemented properties. It's much shorter, right?

class Employee
public int ID{ get; private set; } // read-only
public string FirstName { get; set; }
public int LastName { get; set; }

Code 1.1: Employee class that uses auto-implemented properties


Auto-Implemented Properties is one of the most popular features of C# 3.0 and a lot of developers are really happy with that new feature. but you should know that auto-implemented properties are nothing new except it helps you to boost your productivity.


1. Can "Auto-implemented Properties" improve the performace?

No. It is just helping you to type shorter code but it doesn't improve the performance since the compiler will generate the same as the way what it generates for normal class that doens't have auto-implemented properties.

2. How can I make read-only or write-only auto-implemented properties?

You can make the accessor as private. For example: If you want to make read-only property then You can put "private" in setter. You can read more about that in C# 3.0 specification.

It mentioned like below in C# 3.0 specification ~

When a property is specified as an automatically implemented property, a hidden backing field is automatically available for the property, and the accessors are implemented to read from and write to that backing field.

Because the backing field is inaccessible, it can be read and written only through the property accessors. This means that automatically implemented read-only or write-only properties do not make sense, and are disallowed. It is however possible to set the access level of each accessor differently. Thus, the effect of a read-only property with a private backing field can be mimicked like this:

public class ReadOnlyPoint {
public int X { get; private set; }
public int Y { get; private set; }
public ReadOnlyPoint(int x, int y) { X = x; Y = y; }


Okay. This is all about Auto-Implemented Properties (a.k.a Automatic Properties). If you have any suggestion or comment, please drop a comment in this post. Thank you!


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}