计算计数的IEnumerable(非通用) [英] Calculating Count for IEnumerable (Non Generic)
本文介绍了计算计数的IEnumerable(非通用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能帮我用计数
的扩展方法的IEnumerable
(非通用接口)。
我知道这是不支持LINQ但如何手动写呢?
解决方案
yourEnumerable.Cast<对象>()COUNT()
应用于对性能的注释:的
我觉得这是premature优化一个很好的例子,但在这里你去:
静态类EnumerableExtensions
{
公共静态诠释计数(这IEnumerable的源)
{
INT RES = 0;
的foreach(源VAR项)
水库++;
返回水库;
}
}
Can anyone help me with a Count
extension method for IEnumerable
(non generic interface).
I know it is not supported in LINQ but how to write it manually?
解决方案
yourEnumerable.Cast<object>().Count()
To the comment about performance:
I think this is a good example of premature optimization but here you go:
static class EnumerableExtensions
{
public static int Count(this IEnumerable source)
{
int res = 0;
foreach (var item in source)
res++;
return res;
}
}
这篇关于计算计数的IEnumerable(非通用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文