C#不是只有IEnumerable和IEnumerator才能被foreach

经常我们会写如下的代码:

foreach (var i in list)
{
    // ......
}

然后一问为什么可以 foreach,大多都会回复因为这个 list 实现了 IEnumerable 或者 IEnumerator.

但是实际上,如果想要一个对象可被 foreach,只需要提供一个 GetEnumerator() 方法,并且 GetEnumerator() 返回的对象包含一个 bool MoveNext() 方法加一个 Current 属性即可。

class MyEnumerator<T>
{
    public T Current { get; private set; }
    public bool MoveNext()
    {
        throw new NotImplementedException();
    }
}
    
class MyEnumerable<T>
{
    public MyEnumerator<T> GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

class Program
{
    public static void Main()
    {
        var x = new MyEnumerable<int>();
        foreach (var i in x)
        {
            // ......
        }
    }
}

不是只有 IAsyncEnumerable 和 IAsyncEnumerator 才能被 await foreach

同上,但是这一次要求变了,GetEnumerator() 和 MoveNext() 变为 GetAsyncEnumerator() 和 MoveNextAsync()

其中 MoveNextAsync() 返回的东西应该是一个 Awaitable<bool>,至于这个 Awaitable 到底是什么,它可以是 Task/ValueTask,也可以是其他的或者你自己实现的。

class MyAsyncEnumerator<T>
{
    public T Current { get; private set; }
    public MyTask<bool> MoveNextAsync()
    {
        throw new NotImplementedException();
    }
}
    
class MyAsyncEnumerable<T>
{
    public MyAsyncEnumerator<T> GetAsyncEnumerator()
    {
        throw new NotImplementedException();
    }
}

class Program
{
    public static async Task Main()
    {
        var x = new MyAsyncEnumerable<int>();
        await foreach (var i in x)
        {
            // ......
        }
    }
}