Object Initializers

I’ve always wondered what the syntax is called which allows you to instantiate an object and specify property values on the fly. This example demonstrates the instantiation of a Dog object and specifying property values on the fly:

class Dog

{

    public string Name { get; set; }

    public int Age { get; set; }

}

class Program

{

    // Instantiate a Dog via Object Initialization

    Dog dog = new Dog { Name = “Tiras”, Age = 12 };

}

This syntax is called Object Initialization.

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