计算计数的IEnumerable(非通用)IEnumerable

2023-09-02 01:55:26 作者:第四晚心情

谁能帮我用计数的扩展方法的IEnumerable (非通用接口)。

我知道这是不支持LINQ但如何手动写呢?

解决方案

  yourEnumerable.Cast<对象>()COUNT()
 

应用于对性能的注释:的

我觉得这是premature优化一个很好的例子,但在这里你去:

 静态类EnumerableExtensions
{
    公共静态诠释计数(这IEnumerable的源)
    {
        INT RES = 0;

        的foreach(源VAR项)
            水库++;

        返回水库;
    }
}
 

opencv目标检测之canny算法

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;
    }
}

 
精彩推荐
图片推荐