Tutorial by Examples

Object initializers are handy when you need to create an object and set a couple of properties right away, but the available constructors are not sufficient. Say you have a class public class Book { public string Title { get; set; } public string Author { get; set; } // the rest o...
Object initializers are the only way to initialize anonymous types, which are types generated by the compiler. var album = new { Band = "Beatles", Title = "Abbey Road" }; For that reason object initializers are widely used in LINQ select queries, since they provide a convenie...
You can combine object initializers with constructors to initialize types if necessary. Take for example a class defined as such: public class Book { public string Title { get; set; } public string Author { get; set; } public Book(int id) { //do things } // the...

Page 1 of 1