开发者

Does the non-generic version of IEnumerable support deferred execution?

开发者 https://www.devze.com 2023-02-03 04:02 出处:网络
If so, on what .NET Framework versions is it supported? I have tested this on .NET Framework 4.0 and it works fine:

If so, on what .NET Framework versions is it supported?

I have tested this on .NET Framework 4.0 and it works fine:

using System;
using System.Collections.Generic;

public class TestClass
{
    public IEnumerable Defer()
    {
        yield return 1;
        yi开发者_StackOverflow中文版eld return 2;
        yield return 3;
    }
}


Yes, it is supported ever since the yield keyword was. The only difference is that it's more or less IEnumerable<object>, which might lead to inefficiencies if it has to do boxing. Other than that, it's exactly the same.


As the yield keywords are reduced to compiler trickery, presumably this should work. It certainly works for the 2.0 runtime; I'd hesitate to make any statements about 1.1, however.


The non-generic IEnumerable does not implement IDisposable. It may be that VB.Net and C# will duck-type either IDisposable or the .Dispose() method when using an enumerator that does not support IEnumerable(Of T), but one can certainly not rely upon all consumers of the non-generic IEnumerable to do so. If the consumer of an enumerable does not properly .Dispose() it, execution of the enumerator, including explicit or implicit finally clauses, will be abandoned.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号