Enumerable.Range实施 [英] Enumerable.Range implementation

查看:211
本文介绍了Enumerable.Range实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是的 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?

推荐答案

的<一个href="http://stackoverflow.com/questions/408452/why-enumerable-range-is-faster-than-a-direct-yield-loop/408494#408494">accepted回答这个<一href="http://stackoverflow.com/questions/408452/why-enumerable-range-is-faster-than-a-direct-yield-loop">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.

这篇关于Enumerable.Range实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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