From the course: .NET Essentials: Working with LINQ

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Object initializers

Object initializers

- In this video, we'll look at Object Initializer, which is a way of setting properties on a type on the same line of code that you instantiate that type. For this example, I'm using this classical house, it has three properties, the number of floors, the number of bathrooms and the external paint color represented as a string. The way you instantiate the house class is shown here on line five use the new keyword. And this is calling the default parameter list constructor. Of course you can create more than one constructor in the class, so I could create a constructor that had the number of floors as a parameter or the external paint color, or a combination of those two, and the more properties you add to a type, the more constructor overloads you need to create, or you might have to start working with optional parameters. In this case, this house class has none of those, So there's no way for me to use the constructor to…

Contents