Skip to content

Commit 6d749fd

Browse files
Merge pull request #68 from messerli-informatik-ag/index-initializers
Index initializers
2 parents 310e3d9 + c601820 commit 6d749fd

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

documentation/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
* [Miscellaneous](./miscellaneous.md)
1919
* [Comments](./comments.md)
2020
* [Commit Message](./commit-messages.md)
21+
* [Object Initializer](./object-initializer.md)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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*

0 commit comments

Comments
 (0)