object initializer


C# 3.0 introduced object initializers which allow you to instantiate a class and its properties in one statement.

Example of instantiating a class without object initializers:

Animal animal = new Animal();

animal.Color = “Brown”;

animal.Age = 2;

And now with object initializers:

Animal animal = new Animal { Color=”Brown”, Age=2 };

No comments:

Post a Comment