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

  • Home
  • SEARCH
  • 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 393255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:14:22+00:00 2026-05-12T16:14:22+00:00

Say I have a class that implements IEnumerable<T> . It currently uses the yield

  • 0

Say I have a class that implements IEnumerable<T>. It currently uses the yield keyword in the GetEnumerator() method. But now I need to do a bit more, for example I would like to clean up after myself. To do this, unless I have overlooked anything, I need to implement the IEnumerator<T> interface. But where would you say I should do that?

Should the class itself implement it and GetEnumerator() return this? Or would it be better to hide it in a private class? Or should it maybe just be an entirely different class? What are some common practices to this?

  • 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-05-12T16:14:22+00:00Added an answer on May 12, 2026 at 4:14 pm

    If all you need to do is clean up some resources when the enumerator is disposed of, like at the end of a foreach loop, then you can do that with what you have, just add a try/finally block to your iterator method.

    Like this:

    public IEnumerator<T> GetEnumerator()
    {
        try
        {
            // your iterator code here
        }
        finally
        {
            // cleanup code here
        }
    }
    

    That’s all there is to it.

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

Sidebar

Related Questions

Let's say I have a class that implements ArrayAccess but doesn't allow the addition
Let's say I have a class that implements the IDisposable interface. Something like this:
I have a class A that implements IEquatable<>, using its fields (say, A.b and
Let's say I have a base class that implements the INotifyPropertyChanged interface and I
Let's say I have a class that has a member called data which is
Let's say I have a class that requires configuration, dependency injection etc. public class
In Java, say you have a class that wraps an ArrayList (or any collection)
Let's say I have one class Foo that has a bunch of logic in
Say I have a class which represents a person, a variable within that class
I have a class that contains a dynamically allocated array, say class A {

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.