Search

Mar 14, 2009

Object and Collection Initialization in C# 3.0

My last post I speak about Automatic Properties, and how we can use them. Here we will discuss the Object and Collection Initialization in C# 3.0. In general we are do initialize our property either inside constructor [standard method] or by calling some methods or assigning value directly to public property. Let's see how.

public class Test
{

public class InitializeTest
{
public int Id { get; private set; }
public string Name { get; private set; }

public InitializeTest()
{

}

//Initialize using constructor
public InitializeTest(int intId, string strName)
{
Id = intId;
Name = strName;
}

//Initialize using method
public void SetId(int intId)
{
Id = intId;
}
}

static void Start()
{
//Initialize using constructor
InitializeTest objInitializeTest1 = new InitializeTest(1, "a");

//Initialize using method
InitializeTest objInitializeTest2 = new InitializeTest();
objInitializeTest2.SetId(1);
}
}

Lets talk about initialize using constructor, we have two constructor, one is default and another is accepting two arguments. so far so good. Lets say I have added new property into my class, then? have to create new overloaded constructor? or modify two argument constructor with three which will raise few more errors!!! First one will be better option.

public char Sex { get; set; }

//Initialize using constructor
public InitializeTest(int intId, string strName) : this(intId, strName, 'M') { }

//Initialize using constructor
public InitializeTest(int intId, string strName, char cSex)
{
Id = intId;
Name = strName;
Sex = cSex;
}

Now to avoid this lets see what is in Object Initialization provided by C# 3.0. We can simply use single argument constructor to initialize the member of class, lets add one more property to class name Birthdate.

public class InitializeTest
{
public int Id { get; set; }
public string Name { get; set; }
public char Sex { get; set; }
public DateTime Birthdate { get; set; }
.
.
.
.
}

You may notice here that I have removed private scope why? I will explain it later.

static void Start()
{
InitializeTest objInitializeTest3 = new InitializeTest() { Id = 1, Birthdate = DateTime.Now, Name = "Name", Sex = 'M' };
}

That's it!!! initialize the value of property right after constructor inside the curly braces. You can also assign few or complete list. Now if we make Id property private we can't use here [outside of the class] that is the reason for removing private to get accessor. How it works?

The syntaxes used to initialize an object (standard and object initializers) are equivalent after code is compiled. Object initializer syntax produces a call to a constructor for the specified type (either a reference or value type): this is the default constructor whenever you do not place a parenthesis between the type name and the open bracket. If that constructor makes assignments to the member fields successively initialized, the compiler still performs that work, although the assignment might not be used. An object initializer does not have an additional cost if the called constructor of the initialized type is empty

This can be done on parameterize constructor as well.

We seen object initialization, same was we can achieve collection initialization. Let's see how. First lets add one generic collection to out class and then will do initialization.

public class InitializeTest
{
.
.
public List<string> FamilyMembers { get; set; }
.
.
}
.
.
static void Start()
{
InitializeTest objInitializeTest4 =
new InitializeTest() { FamilyMembers = { "First", "Second", "Third" } };
}
.
.

Its really easy and we have eliminated quite a bit of typing.

No comments: