Like two overlapping line segments, we can find infinite points of intersection. To enumerate all these points might not make sense, and we might just want to present that this collection is infinity.

Floating point numbers have defined NegativeInfinity and PositiveInfinity. A number which represents count or ordinal seem not necessary to use floating point numbers, however, integers are not defined something to represent infinity.

So I tried to implement an infinite enumerable. But I suddenly get confused with the term "enumerable" ..

Is there a better way to solve this problem? And is an infinite enumerable still enumerable?

  • Code

    public partial class Infinity: IEnumerable<object> {
        IEnumerator<object> IEnumerable<object>.GetEnumerator() {
            for(; ; )
                yield return Infinity.Enumerable;
        }
    
        public IEnumerator GetEnumerator() {
            for(; ; )
                yield return Infinity.Enumerable;
        }
    
        public Infinity LongCount(
            Func<object, bool> predicate=default(Func<object, bool>)) {
            return Infinity.Enumerable;
        }
    
        public Infinity Count(
            Func<object, bool> predicate=default(Func<object, bool>)) {
            return Infinity.Enumerable;
        }
    
        public static readonly Infinity Enumerable=new Infinity();
    }
    

Edit:

Thanks for answering. I'm not confused with IEnumerable and IEnumerator. GetEnumerator methods return Infinity.Enumerable is because I do not want to declare an extra dummy object such as:

static readonly object dummy=new object();

and yield return dummy in GetEnumerator methods.

有帮助吗?

解决方案

And is an infinite enumerable still enumerable?

Enumerable, in this sense, is based off the second definition of enumerate:

to specify one after another

It is not referring to the (more common outside of computing) definition whereby it effectively means "able to be counted."

In this sense, an infinite series can definitely be listed one item after another, and qualifies as an enumerable.

That being said, I don't see the purpose behind your code in this example. Infinite enumerables are typically representing something like a stream of data without an end, or other sources where there is no "end", but the potential to continually pull information.

其他提示

The positive natural numbers are infinite and clearly enumerable (1, 2, 3, …). The concept is well-defined even outside of C#.

Your class however has problems because you are confusing the IEnumerable and IEnumerator interface. The GetEnumerator method returns only one enumerator. That is infinite.

An easy implementation of an infinite IEnumerable in C# (as a method rather than a class) looks as follows:

IEnumerable<int> Infinite() {
    int i = 1;
    while (true)
        yield return i++;
}

Caveat: int overflows at some point. However, by default C# will then simply loop back to negative numbers.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top