You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _rules/1523.md
+7-16Lines changed: 7 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,32 +4,23 @@ rule_category: maintainability
4
4
title: Favor object and collection initializers over separate statements
5
5
severity: 2
6
6
---
7
-
Instead of:
8
-
9
-
var startInfo = new ProcessStartInfo("myapp.exe");
10
-
startInfo.StandardOutput = Console.Output;
11
-
startInfo.UseShellExecute = true;
12
-
13
-
var countries = new List();
14
-
countries.Add("Netherlands");
15
-
countries.Add("United States");
16
-
17
-
var countryLookupTable = new Dictionary<string, string>();
18
-
countryLookupTable.Add("NL", "Netherlands");
19
-
countryLookupTable.Add("US", "United States");
20
-
21
-
Use [Object and Collection Initializers](http://msdn.microsoft.com/en-us/library/bb384062.aspx):
7
+
Use [Object and Collection Initializers](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers):
22
8
23
9
var startInfo = new ProcessStartInfo("myapp.exe")
24
10
{
25
11
StandardOutput = Console.Output,
26
12
UseShellExecute = true
27
13
};
28
14
29
-
var countries = new List { "Netherlands", "United States" };
15
+
var countries = new List<string> { "Netherlands", "United States" };
30
16
31
17
var countryLookupTable = new Dictionary<string, string>
32
18
{
33
19
["NL"] = "Netherlands",
34
20
["US"] = "United States"
35
21
};
22
+
23
+
This also applies to arrays and the spread operator (C# 12+):
0 commit comments