Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8183613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:12:57+00:00 2026-06-07T01:12:57+00:00

Just wondering why Enumerable.Range implements IDisposable . I understand why IEnumerator<T> does, but IEnumerable<T>

  • 0

Just wondering why Enumerable.Range implements IDisposable.

I understand why IEnumerator<T> does, but IEnumerable<T> doesn’t require it.


(I discovered this while playing with my .Memoise() implementation, which has statement like

if (enumerable is IDisposable)
    ((IDisposable)enumerable).Dispose();

in its “source finished” method that I had placed a breakpoint on out of curiousity, and was triggered by a test.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:13:01+00:00Added an answer on June 7, 2026 at 1:13 am

    Enumerable.Range uses yield return in its method body. The yield return statement produces an anonymous type that implements IDisposable, under the magic of the compiler, like this:

    static IEnumerable<int> GetNumbers()
    {
        for (int i = 1; i < 10; i += 2)
        {
            yield return i;
        }
    }
    

    After being compiled, there is an anonymous nested class like this:

    [CompilerGenerated]
    private sealed class <GetNumbers>d__0 
       : IEnumerable<int>, IEnumerable, IEnumerator<int>, IEnumerator, IDisposable
    {
        //the implementation
        //note the interface is implemented explicitly
        void IDisposable.Dispose() { }
    }
    

    so the result is a IDisposable. In this example, the Dispose method leaves empty. I think the reason is that there is nothing need to be disposed. If you yield return a type that contains unmanaged resources, you may get a different compiling result. (NOT SURE about it)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if anyone can optimize this usortMonths() function to be better? Basically I
Just wondering if anyone can help me with this slight issue. I'm writing a
just wondering what the subtle difference between an array and a range is. I
I sent this question to ServerFault, but I'm wondering if it's really a programming
Just wondering if this is the most efficient method of doing this? is there
This is just a curiosity question I was wondering if anyone had a good
just wondering if I have a webpage that generates a pdf, but could take
Just wondering from where does the snooze pop up gets triggered when using ScheduledNotification,
just wondering if there is a way to reduce the amount of code needed
Just wondering: I'm trying to set up an adaptive image handler in Coldfusion8, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.