ReSharper 2024.1 Help

Code inspection: Use collection expression syntax

Collection expressions, introduced in C#12, provide a more concise syntax for collection initializations. Therefore, ReSharper suggests using this syntax instead of the legacy collection initializer syntax:

List<string> numbers = new List<string> {"one", "two"};
List<string> numbers = ["one", "two"];

If the collection initializer is immediately followed by Add() calls on the collection, the quick-fix will also integrate these calls into the collection expression:

List<string> numbers = new() { "one", "two" }; numbers.Add("three"); numbers.Add("four");
List<string> numbers = [ "one", "two", "three", "four" ];
Last modified: 08 April 2024