计数来自IEnumerable< T>的项目。没有迭代? [英] Count the items from a IEnumerable<T> without iterating?

查看:67
本文介绍了计数来自IEnumerable< T>的项目。没有迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private IEnumerable<string> Tables
{
    get
    {
        yield return "Foo";
        yield return "Bar";
    }
}

假设我要对这些内容进行迭代并写类似正在处理#m个#n。

Let's say I want iterate on those and write something like processing #n of #m.

有没有一种方法可以在进行主迭代之前不进行迭代就找出m的值?

Is there a way I can find out the value of m without iterating before my main iteration?

I希望我能说清楚。

推荐答案

IEnumerable 不支持此功能。这是设计使然。 IEnumerable 使用惰性评估来获取您需要的元素,然后才需要它们。

IEnumerable doesn't support this. This is by design. IEnumerable uses lazy evaluation to get the elements you ask for just before you need them.

如果您想知道您可以使用 ICollection< T> 来访问项目的数量,它具有 Count 属性。

If you want to know the number of items without iterating over them you can use ICollection<T>, it has a Count property.

这篇关于计数来自IEnumerable&lt; T&gt;的项目。没有迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆