| Start |
|---|
| Simple usage 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 To initialize a new instance of the class with an initializer: This is equivalent to |
| Usage with non-default constructors You can combine object initializers with constructors to initialize types if necessary. Take for example a class defined as such: This will first instantiate a |
| Usage with anonymous types Object initializers are the only way to initialize anonymous types, which are types generated by the compiler. For that reason object initializers are widely used in LINQ select queries, since they provide a convenient way to specify which parts of a queried object you are interested in. Syntax
RemarksThe constructor parentheses can only be omitted if the type being instantiated has a default (parameterless) constructor available. |
Read Article
https://develop.horje.com/learn/1434/reference