Collections - prodot/ReCommended-Extension GitHub Wiki
Suggest using new T[n] to improve code readability, where n is the element count.
-
new T[] { d, default, default(T) }where d is the default value for theT(null,0,false,'\0', ornew T()whereTis known to be value type) -
new[] { d, default, default(T) }where d is the default value for theT -
new[] { null as T, default }whereTis a reference type -
T[] variable = { d }variable or field declaration with an initialization where d is the default value for theT(or the default expression) -
T[] Property { get; } = { d };auto property declaration with an initialization where d is the default value for theT(or the default expression) -
T[] Property { get; set; } = { d };auto property declaration with an initialization where d is the default value for theT(or the default expression) -
[d, default, default(T)]where d is the default value for theTand the expression is target-typed to a following type:T[],IEnumerable<T>,IReadOnlyCollection<T>, orIReadOnlyList<T>
Tip
A quick-fix is available to replace the initialization.
Important
Known Limitations
- Only single-dimension arrays are supported
- Jagged arrays are not supported
Suggest using the Array.Empty<T>() method or the empty collection expression ([]) to reuse the immutable instance.
-
new T[] { }expressions -
T[] variable = { }variable or field declaration with an initialization -
T[] Property { get; } = { };auto property declaration with an initialization -
T[] Property { get; set; } = { };auto property declaration with an initialization
Note
- The suggestion is made only if the used framework has the
Array.Empty<T>()method. - Collection expressions require C# 12 or newer.
Suggests using collection expressions if the target type is one of the following types:
T[]-
IEnumerable<T>(with a note that a compiler-synthesized read-only collection might be used) -
IReadOnlyCollection<T>(with a note that a compiler-synthesized read-only collection might be used) -
IReadOnlyList<T>(with a note that a compiler-synthesized read-only collection might be used) -
ICollection<T>(with a note thatList<T>will be used) -
IList<T>(with a note thatList<T>will be used)
Suggests using collection expressions if the target type is one of the following types:
-
List<T>(only if the initial capacity is not specified) -
IEnumerable<T>(with a note that a compiler-synthesized read-only collection might be used) -
IReadOnlyCollection<T>(with a note that a compiler-synthesized read-only collection might be used) -
IReadOnlyList<T>(with a note that a compiler-synthesized read-only collection might be used) -
ICollection<T>(only if the initial capacity is not specified) -
IList<T>(only if the initial capacity is not specified)
Suggests using collection expressions if the target type is one of the following types:
-
HashSet<T>(only if the initial capacity and the comparer are not specified) -
IEnumerable<T>(only if the hash set is empty) -
IReadOnlyCollection<T>(only if the hash set is empty)
Suggests using collection expressions if the target type is one of the following types:
-
Dictionary<K,V>(only if the dictionary is empty and initial capacity and the comparer are not specified)
Suggest using collection expressions if the target type is one of the following types:
T[]IEnumerable<T>IReadOnlyCollection<T>IReadOnlyList<T>-
ICollection<T>(with a note thatList<T>will be used) -
IList<T>(with a note thatList<T>will be used)
Note
The analyzer can be configured or deactivated in the ReSharper Options or Rider Settings dialog.