Edgar Gonzalez
Edgar Gonzalez

Reputation: 1902

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

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;
        yield return 2;
        yield return 3;
    }
}

Upvotes: 3

Views: 361

Answers (3)

supercat
supercat

Reputation: 81237

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.

Upvotes: 1

Ben
Ben

Reputation: 6059

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.

Upvotes: 1

Mike Caron
Mike Caron

Reputation: 14561

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.

Upvotes: 4

Related Questions