Object Initialization Expressions

The object initialization expressions is another new C#3.0 feature. what is really cool about object initialization expressions is that they allow you to intialize new objects without calling the object constructor or set the properties. Below is an example of object initialization expressions.


public class Employee
{
    public int ID;
    public int Age;
    public int Salary;
    public string Name;
}

List employees = new List()
{
    new Employee(){ID=1,Age=27,Name = "John", Salary=3000},
    new Employee(){ID=1,Age=37,Name = "Sara", Salary=5000},
    new Employee(){ID=1,Age=31,Name = "Tommy", Salary=6000},
};

Advertisements

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

%d bloggers like this: