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 85973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:10:24+00:00 2026-05-10T22:10:24+00:00

I noticed that the generic IEnumerator<T> inherits from IDisposable, but the non-generic interface IEnumerator

  • 0

I noticed that the generic IEnumerator<T> inherits from IDisposable, but the non-generic interface IEnumerator does not. Why is it designed in this way?

Usually, we use foreach statement to go through a IEnumerator<T> instance. The generated code of foreach actually has try-finally block that invokes Dispose() in finally.

  • 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. 2026-05-10T22:10:24+00:00Added an answer on May 10, 2026 at 10:10 pm

    Basically it was an oversight. In C# 1.0, foreach never called Dispose 1. With C# 1.2 (introduced in VS2003 – there’s no 1.1, bizarrely) foreach began to check in the finally block whether or not the iterator implemented IDisposable – they had to do it that way, because retrospectively making IEnumerator extend IDisposable would have broken everyone’s implementation of IEnumerator. If they’d worked out that it’s useful for foreach to dispose of iterators in the first place, I’m sure IEnumerator would have extended IDisposable.

    When C# 2.0 and .NET 2.0 came out, however, they had a fresh opportunity – new interface, new inheritance. It makes much more sense to have the interface extend IDisposable so that you don’t need an execution-time check in the finally block, and now the compiler knows that if the iterator is an IEnumerator<T> it can emit an unconditional call to Dispose.

    EDIT: It’s incredibly useful for Dispose to be called at the end of iteration (however it ends). It means the iterator can hold on to resources – which makes it feasible for it to, say, read a file line by line. Iterator blocks generate Dispose implementations which make sure that any finally blocks relevant to the ‘current point of execution’ of the iterator are executed when it’s disposed – so you can write normal code within the iterator and clean-up should happen appropriately.


    1 Looking back at the 1.0 spec, it was already specified. I haven’t yet been able to verify this earlier statement that the 1.0 implementation didn’t call Dispose.

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

Sidebar

Related Questions

I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but
i noticed that paypal displays a very different favicon, one that's not just a
Have you noticed that every 10 questions on this site is about jQuery? Anyway...
I noticed that Google maps is providing directions in my local language (hungarian) when
I noticed that my Designer.vb file of one of my forms has a lot
We noticed that lots of bugs in our software developed in C# (or Java)
I noticed that there is one change about ASP.NET Routing. I cannot understand why
Just noticed that the style of the navigation menu on windows.com is just what
I noticed that doxygen uses the graphviz library for creating diagrams. Have you ever
I noticed that I can start a program with it's associated handler by writing

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.