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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:41:02+00:00 2026-05-13T06:41:02+00:00

I have a List class, and I would like to override GetEnumerator() to return

  • 0

I have a List class, and I would like to override GetEnumerator() to return my own Enumerator class. This Enumerator class would have two additional properties that would be updated as the Enumerator is used.

For simplicity (this isn’t the exact business case), let’s say those properties were CurrentIndex and RunningTotal.

I could manage these properties within the foreach loop manually, but I would rather encapsulate this functionality for reuse, and the Enumerator seems to be the right spot.

The problem: foreach hides all the Enumerator business, so is there a way to, within a foreach statement, access the current Enumerator so I can retrieve my properties? Or would I have to foreach, use a nasty old while loop, and manipulate the Enumerator myself?

  • 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-13T06:41:03+00:00Added an answer on May 13, 2026 at 6:41 am

    Strictly speaking, I would say that if you want to do exactly what you’re saying, then yes, you would need to call GetEnumerator and control the enumerator yourself with a while loop.

    Without knowing too much about your business requirement, you might be able to take advantage of an iterator function, such as something like this:

        public static IEnumerable<decimal> IgnoreSmallValues(List<decimal> list)
        {
            decimal runningTotal = 0M;
            foreach (decimal value in list)
            {
                // if the value is less than 1% of the running total, then ignore it
                if (runningTotal == 0M || value >= 0.01M * runningTotal)
                {
                    runningTotal += value;
                    yield return value;
                }
            }
        }
    

    Then you can do this:

            List<decimal> payments = new List<decimal>() {
                123.45M,
                234.56M,
                .01M,
                345.67M,
                1.23M,
                456.78M
            };
    
            foreach (decimal largePayment in IgnoreSmallValues(payments))
            {
                // handle the large payments so that I can divert all the small payments to my own bank account.  Mwahaha!
            }
    

    Updated:

    Ok, so here’s a follow-up with what I’ve termed my “fishing hook” solution. Now, let me add a disclaimer that I can’t really think of a good reason to do something this way, but your situation may differ.

    The idea is that you simply create a “fishing hook” object (reference type) that you pass to your iterator function. The iterator function manipulates your fishing hook object, and since you still have a reference to it in your code outside, you have visibility into what’s going on:

        public class FishingHook
        {
            public int Index { get; set; }
            public decimal RunningTotal { get; set; }
            public Func<decimal, bool> Criteria { get; set; }
        }
    
        public static IEnumerable<decimal> FishingHookIteration(IEnumerable<decimal> list, FishingHook hook)
        {
            hook.Index = 0;
            hook.RunningTotal = 0;
            foreach(decimal value in list)
            {
                // the hook object may define a Criteria delegate that
                // determines whether to skip the current value
                if (hook.Criteria == null || hook.Criteria(value))
                {
                    hook.RunningTotal += value;
                    yield return value;
                    hook.Index++;
                }
            }
        }
    

    You would utilize it like this:

            List<decimal> payments = new List<decimal>() {
                123.45M,
                .01M,
                345.67M,
                234.56M,
                1.23M,
                456.78M
            };
    
            FishingHook hook = new FishingHook();
    
            decimal min = 0;
            hook.Criteria = x => x > min; // exclude any values that are less than/equal to the defined minimum
            foreach (decimal value in FishingHookIteration(payments, hook))
            {
                // update the minimum
                if (value > min) min = value;
    
                Console.WriteLine("Index: {0}, Value: {1}, Running Total: {2}", hook.Index, value, hook.RunningTotal);
            }
            // Resultint output is:
            //Index: 0, Value: 123.45, Running Total: 123.45
            //Index: 1, Value: 345.67, Running Total: 469.12
            //Index: 2, Value: 456.78, Running Total: 925.90
            // we've skipped the values .01, 234.56, and 1.23
    

    Essentially, the FishingHook object gives you some control over how the iterator executes. The impression I got from the question was that you needed some way to access the inner workings of the iterator so that you could manipulate how it iterates while you are in the middle of iterating, but if this is not the case, then this solution might be overkill for what you need.

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

Sidebar

Related Questions

i neeed something like this in C#.. have list in class but decide what
Let's say I have a class like this (and also further assume that all
I have List of particular class. I want to save this List at a
I have a list of class items List<MyClass> I have a seperate object that
That is, I have a list of class ' Entity ', which is an
I have a list view that is displaying data using the gridview. This list
I have a custom listPreference I would like to display a list of apps
I have a class that maintains list of objects of another class. List of
I have a base class with a virtual method, and multiple subclasses that override
I need help with the following problem: I have a class with two properties.

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.