Skip to content

Commit 736af31

Browse files
Dennis DoomenCopilot
authored andcommitted
Update AV1523 guideline
Split from #298. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a0b0bea commit 736af31

1 file changed

Lines changed: 19 additions & 24 deletions

File tree

_rules/1523.md

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,27 @@ rule_category: maintainability
44
title: Favor object and collection initializers over separate statements
55
severity: 2
66
---
7-
Instead of:
7+
Use [Object and Collection Initializers](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers):
88

9-
var startInfo = new ProcessStartInfo("myapp.exe");
10-
startInfo.StandardOutput = Console.Output;
11-
startInfo.UseShellExecute = true;
9+
```csharp
10+
var startInfo = new ProcessStartInfo("myapp.exe")
11+
{
12+
StandardOutput = Console.Output,
13+
UseShellExecute = true
14+
};
1215

13-
var countries = new List();
14-
countries.Add("Netherlands");
15-
countries.Add("United States");
16+
new List<string> countries = ["Netherlands", "United States"];
1617

17-
var countryLookupTable = new Dictionary<string, string>();
18-
countryLookupTable.Add("NL", "Netherlands");
19-
countryLookupTable.Add("US", "United States");
18+
var countryLookupTable = new Dictionary<string, string>
19+
{
20+
["NL"] = "Netherlands",
21+
["US"] = "United States"
22+
};
23+
```
2024

21-
Use [Object and Collection Initializers](http://msdn.microsoft.com/en-us/library/bb384062.aspx):
25+
This also applies to arrays and the spread operator (C# 12+):
2226

23-
var startInfo = new ProcessStartInfo("myapp.exe")
24-
{
25-
StandardOutput = Console.Output,
26-
UseShellExecute = true
27-
};
28-
29-
var countries = new List { "Netherlands", "United States" };
30-
31-
var countryLookupTable = new Dictionary<string, string>
32-
{
33-
["NL"] = "Netherlands",
34-
["US"] = "United States"
35-
};
27+
```csharp
28+
string[] moreCountries = ["Belgium", "Germany"];
29+
string[] allCountries = [..countries, ..moreCountries];
30+
```

0 commit comments

Comments
 (0)