File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 1818* [ Miscellaneous] ( ./miscellaneous.md )
1919* [ Comments] ( ./comments.md )
2020* [ Commit Message] ( ./commit-messages.md )
21+ * [ Object Initializer] ( ./object-initializer.md )
Original file line number Diff line number Diff line change 1+ # Initializer
2+
3+ ## Index Initializers
4+
5+ Use index initializers with dictionary.
6+ This elegantly prevents uninitialized dictionaries.
7+ See [ Object and Collection Initializers (C# Programming Guide)] .
8+
9+ [ Object and Collection Initializers (C# Programming Guide) ] : https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers#object-initializers-with-collection-read-only-property-initialization
10+
11+ ``` csharp
12+ var dict = new Dictionary <string , int >
13+ {
14+ [" key1" ] = 1 ,
15+ [" key2" ] = 50 ,
16+ };
17+ ```
18+ * Good example*
19+
20+ ``` csharp
21+ var dict = new Dictionary <string , int >();
22+ dict [" key1" ] = 1 ;
23+ dict [" key2" ] = 50 ;
24+ ```
25+ * Not so good example*
26+
27+ ``` csharp
28+ var dict = new Dictionary <string , int >
29+ {
30+ { " key1" , 1 },
31+ { " key2" , 50 },
32+ };
33+ ```
34+ * Deprecated way example*
You can’t perform that action at this time.
0 commit comments