代码检查:使用集合的 count 属性
如果集合具有 计数 属性(或 长度 属性),JetBrains Rider 建议使用该属性而不是 计数() 方法。 该属性的运行速度更快(要了解原因,请阅读下面 另请参阅 中链接的 StackOverflow 答案)。 此类替换仅适用于实现 ICollection<T> 或其派生接口的集合。
对于数组,JetBrains Rider 建议将 计数() 替换为 长度:
public int BooksCount(string[] books)
{
return books.Count();
}
public int BooksCount(string[] books)
{
return books.Length;
}
对于其他集合类型,JetBrains Rider 建议将 计数() 替换为 计数:
public int ToysCount(IList<string> toys)
{
return toys.Count();
}
public int ToysCount(IList<string> toys)
{
return toys.Count;
}
最后修改日期: 2025年 9月 26日