Ahmed El-Halwagy’s Blog

Auto Implemented Properties, and Object Initialization Syntax

Posted on: November 25, 2008

As a person who develops applications for business, there’s no doubt that most of the times you have to create classes that hold nothing but data –Which you most of the times would get them generated some way or another). Now imagine that you have a class called EmployeeData which is designed to hold only data of employees. This class can more or less look like the following.

class EmployeeData {

//Member Variables.

private Guid id;

private string name;

private int age;

private string address;

private float salary;

private string position;


//Public access properties.

public Guid ID {

get { return this.id; }

set { this.id = value; }



As you can see here, you define the class variables as private and then encapsulate them in properties to enforce some business logic on the access of these variables. However, most of the times your properties don’t have any business logic code or validation in either of their Getter or Setter accessors. Though the properties are totally useless in this case, you still have to implement them just to have the OO look. Auto Implemented Properties is a capability added to C#3.0 and higher to save you some keystrokes (actually a lot of key strokes). If you have to implement a property to provide access to a variable in your class without having any code to run while getting or setting a value to this variable, use auto implemented property as follows.

public Guid ID { get; set; }

public string Name { get; set; }

public int Age { get; set; }

In the following code, I’ve declared three public properties ID, Name, and Age. I took advantage of the auto implemented properties syntax and saved my self some keystrokes by simply not adding a get and set bloc(though the accessors themselves already exist). The magic part here is the “;” semicolon after the get and the set accessor; this semicolon indicates that the compiler will automatically generate a private field with the type of the property (System.GUID, or System.String, or System.Int32 in our case) and will have the get and set retrieves and sets a value to that automatically generated variable. It worth mentioning that you can’t reference this variable directly, you have to use the property to access it. Also the name of this automatically generated variable is a really compiler-kina names (e.g. “x3ydfa34343adfa343”). The auto implemented properties syntax does not add any magic to your code, it simply saves you some keystrokes by instructing the compiler to generate a variable to you and allows access to this variable through your properties.

2- Object Initialization Syntax.

As an OO programmer, creating objects is a huge part of your daily job duties. You most of the times instantiate new objects of your classes or the base library class to work with and mainpulate in order to get your task done. C# 3.0 added a new capability for programmers to easily create instances of objects with different state. For example if you are creating an employee object named John Doe with 25 years of age and works as a Sales Representitive; your code will look like this :

EmployeeData e = new EmployeeData();

e.Name = “John Doe”;

e.Age = 25;

e.Position = “Sales Representative”;


As you can see, it took you 4 lines to create one employee instance. In order to avoid the hassle and save ourselves some time when creating employees we may create some overloaded constructors in the EmployeeData class and instantiate objects using these custom constructors like so:

public EmployeeData(string name, int age, string position) {

this.Name = name;

this.Age = age;

this.Position = position;


//Instantiating an object using the custom constructor
EmployeeData x = new EmployeeData(“John Doe”, 25, “Sales Representative”);

Obviously this saves a lot of keystrokes, precisely 3 lines for each employee. As you can see her the way to get to this was by editing the definition of the EmployeeData class itself. Now the question is How can I achieve this if I don’t have the class source code? Well the answer is-you guessed it-Object Initialization Syntax.
Object initialization syntax allows you to create objects with different state(i.e. different values of properties and public variables) quickly and easily. You simply create the instance of your object followed by a comma delimited list of public fields enclosed by two curly brackets ({ }). So instantiating the same John Doe employee using object initialization syntax would look like the following:

EmployeeData x = new EmployeeData() { Name = “John Doe”, Age = 25, Position = “Sales Representatives” };
ps: you have to define a default constructor in the EmployeeData class in order for the above line to work.

You can also call a custom constructor and then use the object initialization syntax afterwards like so:

EmployeeData x = new EmployeeData(“John Doe”, 25, “Sales Representative”) { ID = 1 };

In the above example we created the same john doe using the custom constructor then added object initialization syntax to the statement to include the id value to be one. Now the created object has the name “John Doe”, Age 25, Position “Sales Representative”, ID 1.

This last code example fires up the question “what is the order of these values?”. Consider the following example.
EmployeeData x = new EmployeeData(“John Doe”, 25, “Sales Representative”) {

Name = “Hussien IBS”,

Age = 45,

Position = “CO”,

ID = GUID.NewGuid()


Now if you printed out the values of x.Age, x.Name, and x.Position, what will the actual values be? Well one fact to know about object initialization syntax is that it works in order (i.e. x.Name will have the value “John Doe” by the custom constructor, then it will be assigned the value “Brack Belick” by the syntax that followed the constructor call. Bottom line here: Object initialization syntax gets executed in orderf from left to right.

Initializing Inner Types:
Let’s assume that we have another object in the system called Office like the following:

class Office {

public string OfficeType { get; set; }

public float OfficeArea { get; set; }


and the EmployeeData has an office object as a member of it. Take a look at the following code:
public Office MyResidence { get; set; }

You can use the object initialization syntax to initialize the inner type as well:
EmployeeData x = new EmployeeData() {

Name = “Husien IBS”,

Age = 45,

Position = “CO”,

ID = Guid.NewGuid(),

MyResidence = new Office() {

OfficeArea = 25.5f,

OfficeType = “Glass Office”



Collection Initializers:

Object initialization syntax allows you to use the array-like initializtion with your types as follows :

List<EmployeeData> enEmps = new List<EmployeeData>() {

new EmployeeData() {

Position = “Developer”,

Name=“Mohammed Kasem”


new EmployeeData() {

Position = “Developer”,

Name=“MOhamed Tolba”



<!–[if !supportLineBreakNewLine]–>




1 Response to "Auto Implemented Properties, and Object Initialization Syntax"

Good Word , We are waiting more from u

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


November 2008
    Dec »



Recent Twitter Updates

%d bloggers like this: