IEnumerable 是否总是意味着集合?

发布于 2024-12-06 01:13:43 字数 115 浏览 3 评论 0原文

关于 IEnumerable 只是一个简单的问题:

IEnumerable 是否始终意味着集合?或者在单个对象上使用它是否合法/可行/可以/什么?

Just a quick question regarding IEnumerable:

Does IEnumerable always imply a collection? Or is it legitimate/viable/okay/whatever to use on a single object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

少跟Wǒ拽 2024-12-13 01:13:43

IEnumerableIEnumerable 接口建议某种序列,但该序列不必是具体的集合。

例如,在这种情况下,底层的具体集合在哪里?

foreach (int i in new EndlessRandomSequence().Take(5))
{
    Console.WriteLine(i);
}

// ...

public class EndlessRandomSequence : IEnumerable<int>
{
    public IEnumerator<int> GetEnumerator()
    {
        var rng = new Random();
        while (true) yield return rng.Next();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

The IEnumerable and IEnumerable<T> interfaces suggest a sequence of some kind, but that sequence doesn't need to be a concrete collection.

For example, where's the underlying concrete collection in this case?

foreach (int i in new EndlessRandomSequence().Take(5))
{
    Console.WriteLine(i);
}

// ...

public class EndlessRandomSequence : IEnumerable<int>
{
    public IEnumerator<int> GetEnumerator()
    {
        var rng = new Random();
        while (true) yield return rng.Next();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}
故事和酒 2024-12-13 01:13:43

IEnumerable 始终强制用于单个对象 - 该单个对象始终是零个或多个其他对象的持有者或生产者不一定与 IEnumerable 有任何关系。

IEnumerable 表示一个集合,这是常见的,但不是强制性的。

枚举可以是集合,也可以是生成器、查询,甚至是计算。

生成器:

IEnumerable<int> Generate(
    int initial,
    Func<int, bool> condition,
    Func<int, int> iterator)
{
    var i = initial;
    while (true)
    {
        yield return i;
        i = iterator(i);
        if (!condition(i))
        {
            yield break;
        }
    }
}

查询:

IEnumerable<Process> GetProcessesWhereNameContains(string text)
{
    // Could be web-service or database call too
    var processes = System.Diagnostics.Process.GetProcesses();
    foreach (var process in processes)
    {
        if (process.ProcessName.Contains(text))
        {
            yield return process;
        }
    }
}

计算:

IEnumerable<double> Average(IEnumerable<double> values)
{
    var sum = 0.0;
    var count = 0;
    foreach (var value in values)
    {
        sum += value;
        yield return sum/++count;
    }
}

LINQ 本身就是一系列运算符,它们生成实现不具有任何底层集合的 IEnumerable 的对象。

顺便说一句,好问题!

注意:任何对 IEnumerable 的引用也适用于 IEnumerable,因为后者继承了前者。

It is always and mandatory that IEnumerable is used on a single object - the single object is always the holder or producer of zero or more other objects that do not necessarily have any relation to IEnumerable.

It's usual, but not mandatory, that IEnumerable represents a collection.

Enumerables can be collections, as well as generators, queries, and even computations.

Generator:

IEnumerable<int> Generate(
    int initial,
    Func<int, bool> condition,
    Func<int, int> iterator)
{
    var i = initial;
    while (true)
    {
        yield return i;
        i = iterator(i);
        if (!condition(i))
        {
            yield break;
        }
    }
}

Query:

IEnumerable<Process> GetProcessesWhereNameContains(string text)
{
    // Could be web-service or database call too
    var processes = System.Diagnostics.Process.GetProcesses();
    foreach (var process in processes)
    {
        if (process.ProcessName.Contains(text))
        {
            yield return process;
        }
    }
}

Computation:

IEnumerable<double> Average(IEnumerable<double> values)
{
    var sum = 0.0;
    var count = 0;
    foreach (var value in values)
    {
        sum += value;
        yield return sum/++count;
    }
}

LINQ is itself a series of operators that produce objects that implement IEnumerable<T> that don't have any underlying collections.

Good question, BTW!

NB: Any reference to IEnumerable also applies to IEnumerable<T> as the latter inherits the former.

茶色山野 2024-12-13 01:13:43

是的,IEnumerable 意味着项目的集合或可能的集合。

该名称源自 enumerate,意思是:

  1. 一一提及(许多事物)。
  2. 确定数量。

Yes, IEnumerable implies a collection, or possible collection, of items.

The name is derived from enumerate, which means to:

  1. Mention (a number of things) one by one.
  2. Establish the number of.
跨年 2024-12-13 01:13:43

根据文档,它通过集合公开枚举器。

According to the docs, it exposes the enumerator over a collection.

红颜悴 2024-12-13 01:13:43

您当然可以在单个对象上使用它,但是该对象将被公开为包含单个对象的枚举,即您可以拥有一个IEnumerable单个整数:

IEnumerable<int> items = new[] { 42 };

You can certainly use it on a single object, but this object will then just be exposed as an enumeration containing a single object, i.e. you could have an IEnumerable<int> with a single integer:

IEnumerable<int> items = new[] { 42 };
知足的幸福 2024-12-13 01:13:43

IEnumerable 表示可以枚举的集合,而不是单个项目。查看 MSDN;该接口公开了GetEnumerator(),其中

...[r]返回一个迭代集合的枚举器。

IEnumerable represents a collection that can be enumerated, not a single item. Look at MSDN; the interface exposes GetEnumerator(), which

...[r]eturns an enumerator that iterates through a collection.

清风不识月 2024-12-13 01:13:43

是的,IEnumerable 总是意味着集合,这就是枚举的意思。

单个对象的用例是什么?

我不认为在单个对象上使用它有问题,但为什么要这样做呢?

Yes, IEnumerable always implies a collection, that is what enumerate means.

What is your use case for a single object?

I don't see a problem with using it on a single object, but why do want to do this?

赢得她心 2024-12-13 01:13:43

我不确定你的意思是“集合”还是.NET“ICollection”,但由于其他人只提到了前者,我将提到后者。

http://msdn.microsoft.com/en-us/library/92t2ye13.aspx

根据该定义,所有 ICollection 都是 IEnumerable。但反之则不然。
但大多数数据结构(甚至数组)只是实现这两个接口。

按照这个思路:您可以有一个不公开内部数据结构的汽车站(单个对象),并将 IEnumerable 放在上面。我想。

I'm not sure whether you mean a "collection" or a .NET "ICollection" but since other people have only mentioned the former I will mention the latter.

http://msdn.microsoft.com/en-us/library/92t2ye13.aspx

By that definition, All ICollections are IEnumerable. But not the other way around.
But most data structure (Array even) just implement both interfaces.

Going on this train of thought: you could have a car depot (a single object) that does not expose an internal data structure, and put IEnumerable on it. I suppose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文