更新时间:2022-05-21 15:32:22
在这种特殊情况下,使用的IEnumerable< T>
的形式会更有效率,因为你的只有的需要知道计数。有没有点中存储数据,调整缓冲区等,如果你不需要。
In this particular case, using the IEnumerable<T>
form will be more efficient, because you only need to know the count. There's no point in storing the data, resizing buffers etc if you don't need to.
如果您需要再次使用该结果以任何理由,在列表< T>
的形式会更有效。
If you needed to use the results again for any reason, the List<T>
form would be more efficient.
请注意,无论是 COUNT()
扩展方法和计数
属性将是有效的列表< T>
的实施计数()
检查是否目标序列实现的ICollection< T>
,并使用计数
财产,如果左右。
Note that both the Count()
extension method and the Count
property will be efficient for List<T>
as the implementation of Count()
checks to see if the target sequence implements ICollection<T>
and uses the Count
property if so.
这应该是另一种选择,甚至的更多的高效(虽然只是刚)将调用过载计数
这需要一个委托:
Another option which should be even more efficient (though only just) would be to call the overload of Count
which takes a delegate:
private int GetProviderCount(Type type)
{
return _objectProviders.Count(provider =>
(provider.Key.IsAssignableFrom(type)
|| type.IsAssignableFrom(provider.Key))
&& provider.Value.SupportsType(type));
}
这将避免由其中,
和选择
条款。
(正如马克说,对于少量的数据的性能差异可能是微不足道的反正。)
(As Marc says, for small amounts of data the performance differences will probably be negligible anyway.)