Enumerable.Range实施Enumerable、Range

2023-09-04 00:51:39 作者:输得太彻底

什么是的 precise 的实施Enumerable.Range在.net中的; preferable .NET 4中?它是一个产生for循环?自定义实现(的IEnumerable,IEnumerator的)或?

What is the precise implementation of Enumerable.Range in .Net; preferable .Net 4? Is it a yielded for-loop? A custom implementation (IEnumerable, IEnumerator) or?

推荐答案

的accepted回答这个question应该给你答案:

public static class Enumerable {
    public static IEnumerable<int> Range(int start, int count) {
        var end = start + count;
        for(var current = start; current < end; ++current) {
            yield return current;
        }
    }
}

这是不是的确切的code,因为有很多错误检查等范围方法中的事,并在内部,它调用其他方法,但是,报价code以上是范围程序的精华。

This isn't the exact code, as there is a lot of error checking etc. going on within the Range method, and internally, it calls other methods, however, the quoted code above is the "essence" of the Range routine.

审视code在反射应该为您提供更多的信息

Examining the code in Reflector should provide you with far more information.