Collections - prodot/ReCommended-Extension GitHub Wiki

Array Initializers

Suggest using new T[n] to improve code readability, where n is the element count.

Applies to arrays consisting of default values

  • new T[] { d, default, default(T) } where d is the default value for the T (null, 0, false, '\0', or new T() where T is known to be value type)
  • new[] { d, default, default(T) } where d is the default value for the T
  • new[] { null as T, default } where T is a reference type
  • T[] variable = { d } variable or field declaration with an initialization where d is the default value for the T (or the default expression)
  • T[] Property { get; } = { d }; auto property declaration with an initialization where d is the default value for the T (or the default expression)
  • T[] Property { get; set; } = { d }; auto property declaration with an initialization where d is the default value for the T (or the default expression)
  • [d, default, default(T)] where d is the default value for the T and the expression is target-typed to a following type: T[], IEnumerable<T>, IReadOnlyCollection<T>, or IReadOnlyList<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

Empty Array Initializers

Suggest using the Array.Empty<T>() method or the empty collection expression ([]) to reuse the immutable instance.

Applies to

  • 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.

Array Initializers

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 that List<T> will be used)
  • IList<T> (with a note that List<T> will be used)

List<T> Initializers

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)

Hashset<T> Initializers

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)

Dictionary<K,V> Initializers

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)

Array.Empty<T>() Expressions

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 that List<T> will be used)
  • IList<T> (with a note that List<T> will be used)

Note

The analyzer can be configured or deactivated in the ReSharper Options or Rider Settings dialog.

References

⚠️ **GitHub.com Fallback** ⚠️