Overview of C# Automatic Properties

You are currently viewing Overview of C# Automatic Properties

All .NET programming languages prefer the use of Properties to encapsulate the private fields available in the class. There is nothing too problematic with Properties but sometimes when you need properties simply to assign and return the value stored in a private field it looks quite a lot of work especially if you have to declare properties for 15 to 20 private fields. To automate the process of providing simple encapsulation of field data C# now has a new syntax to generate automatic properties. In this tutorial I will show you how you can create Automatic Properties and how you can use them in your programs.

This new feature of generating automatic properties introduces a new syntax to define a property as you can see from the following example:

class Employee 
{ 
   public int ID { get; set; } 
   public string Name { get; set; } 
}

The above syntax might seem strange to you and it looks like you are creating unimplemented abstract properties which has to be overridden by derived class but this is not the case here because you are not decorating your syntax with the keyword abstract which we use to create abstract property as following syntax shows:

public abstract string Address { get; set; }

When you define automatic property, you need to specify access modifier, data type, property name and empty get/set block. When compiler will compile you class it will automatically generate a private field at the back end with the default implementation of get and set blocks logic. Keep in mind that auto generated private fields are not visible in your program and you can just use the properties as follows. However these private fields can be seen by using ildasm.exe tool.

Employee emp = new Employee(); 
emp.ID = 1; 
emp.Name = "James";

One limitation with automatic properties is that unlike traditional C# properties you cannot create read-only or write-only automatic properties. You have to use the same old traditional syntax if you need such type of property in your class. You may be thinking that you can just simply omit either get or set to achieve this but following two lines will not compile:

public string City { get; }      // will not compile 
public string Country { set; }   // will not compile

You can restrict access on automatic properties just like you do with traditional properties however the syntax is different as you can see in the code below:

public string Email { get; private set; }

Email property is not accessible from outside the class as it is private however its value can be read from outside class because get is not private.

public string Phone { private get; set; }

Phone property can be set from outside class but its value cannot be read because its get is marked private.

I hope now you have basic idea of automatic properties of C#. You should use them as much as possible in your programs to encapsulate many private fields with lot less typing and keystrokes without declaring fields and typing those get and set blocks.

READ ALSO:  Understanding C# Extension Methods

This Post Has One Comment

  1. shiya

    try to make it more userfriendly……………….

Leave a Reply