JetBrains Rider 2024.1 Help

Code Inspection: Use collection’s count property

Category

Potential Code Quality Issues

ID

UseCollectionCountProperty

EditorConfig

resharper_use_collection_count_property_highlighting

Default severity

Suggestion

Language

C#

Requires SWA

No

If a collection has a Count property (or a Length property), JetBrains Rider suggests using the property instead of the Count() method. The property works faster (to see why, read the StackOverflow answers linked in the See Also below). Such a replacement is only possible for collections that implement ICollection<T> or its derived interfaces.

For arrays, JetBrains Rider suggests replacing Count() with Length:

public int BooksCount(string[] books) { return books.Count(); }
public int BooksCount(string[] books) { return books.Length; }

For other collection types, JetBrains Rider suggests replacing Count() with Count:

public int ToysCount(IList<string> toys) { return toys.Count(); }
public int ToysCount(IList<string> toys) { return toys.Count; }
Last modified: 17 April 2024