SIGN UP MEMBER LOGIN:    
ARTICLE

Object and Collection Initializers Feature in C# 3.0

Posted by Kalyan Bandarupalli Articles | Visual C# October 20, 2008
Object Initialization Expressions allows you to initialize an object without invoking the constructor and setting its properties.
Reader Level:

Introduction

C# 3.0 introduced another interesting feature Object and Collection initialization expressions.

If you take Employee Class as an Example:



We can use Object Initialization Expressions in C#3.0 Features to create an Employee as follows:


Now observe the above code we have neither invoked the constructor nor set the any properties directly. The code above is equivalent to the following code:



We can use Collection Initialization Expressions in C#3.0 Features to create a list of Employees as follows:



The advantage of using this feature is that it saves your time for creating a lot of constructors or initializing the individual property.
 

Login to add your contents and source code to this article
share this article :
post comment
 

It is probably a good idea to post the code with the article.

 

Posted by Mahesh Chand Oct 23, 2008

Hi

You are right. Only public setters can be used in the Object Intialization. Thanks for the notice. The ID property should have the Setter value and comma is missing after FirstName property.

Kalyan.B

Posted by Kalyan Bandarupalli Oct 21, 2008

The ID is a private field and it has no public setter, thus you cannot use object initialization, or am I missing something?

Posted by Jes Hansen Oct 21, 2008
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor