| Start |
|---|
| Collection initializers Initialize a collection type with values: Collection initializers are syntactic sugar for Note that the intialization is done atomically using a temporary variable, to avoid race conditions. For types that offer multiple parameters in their This is equivalent to: |
| C# 6 Index Initializers Starting with C# 6, collections with indexers can be initialized by specifying the index to assign in square brackets, followed by an equals sign, followed by the value to assign. Dictionary InitializationAn example of this syntax using a Dictionary: This is equivalent to: The collection initializer syntax to do this before C# 6 was: Which would correspond to: So there is a significant difference in functionality, as the new syntax uses the indexer of the initialized object to assign values instead of using its This would output: `10 was assigned to index 0` |
| Collection initializers in custom classes To make a class support collection initializers, it must implement |
Using collection initializer inside object initializer
The collection property can be readonly and still support collection initializer syntax. Consider this modified example ( A new This works because collection initializers are just syntatic sugar over calls to |
| Collection Initializers with Parameter Arrays You can mix normal parameters and parameter arrays: This syntax is now possible: RemarksThe only requirement for an object to be initialized using this syntactic sugar is that the type implements |
Read Article
https://develop.horje.com/learn/1434/reference