Skip to content

Commit 1602473

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

1 file changed

Lines changed: 7 additions & 16 deletions

File tree

_rules/1523.md

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,23 @@ rule_category: maintainability
44
title: Favor object and collection initializers over separate statements
55
severity: 2
66
---
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):
228

239
var startInfo = new ProcessStartInfo("myapp.exe")
2410
{
2511
StandardOutput = Console.Output,
2612
UseShellExecute = true
2713
};
2814

29-
var countries = new List { "Netherlands", "United States" };
15+
var countries = new List<string> { "Netherlands", "United States" };
3016

3117
var countryLookupTable = new Dictionary<string, string>
3218
{
3319
["NL"] = "Netherlands",
3420
["US"] = "United States"
3521
};
22+
23+
This also applies to arrays and the spread operator (C# 12+):
24+
25+
string[] moreCountries = ["Belgium", "Germany"];
26+
string[] allCountries = [..countries, ..moreCountries];

0 commit comments

Comments
 (0)