Enumerable.Range implementation

后端 未结 4 1647
失恋的感觉
失恋的感觉 2020-12-20 08:21

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?<

4条回答
  •  粉色の甜心
    2020-12-20 09:13

    A slight but significant difference in the Reflector output (as well as the argument check and extra level of internalisation mentioned in CraigTP's answer and its comments):

    public static IEnumerable Range(int start, int count) {
        for(int current = 0; current < count; ++current) {
            yield return start + current;
        }
    }
    

    That is, instead of another local variable, they apply an extra addition for every yield.

提交回复
热议问题