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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:19:41+00:00 2026-05-25T14:19:41+00:00

Simple question, I believe –> It is my understanding that an imaginary IEnumerator object

  • 0

Simple question, I believe –> It is my understanding that an imaginary IEnumerator object is being used when I use a foreach loop over an IEnumerable object. My question is as follows:

How can I “catch” illegal behavior in foreach loops dealing with my objects? I specifically want to see if my original object from which the IEnumerable was created has been modified. If the original has been modified, I want to throw an exception.

My current approach has been to use a version number. This works great if I create an explicit iterator and use a MoveNext() or somesuch, but foreach loops seem to trick my version-based approach.

  • 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-25T14:19:42+00:00Added an answer on May 25, 2026 at 2:19 pm

    The IEnumerable interface is what actualy returns the IEnumerator it is the whole reason this interface exists.

    When the foreach block is call the IEnumerable.GetEnumerator() method is called to get the enumerator.

    If you want to track each item then because they are object types they will be referenced so you will have the update to date version. Within the item you could implement the versioning that you mentioned like so.

    public class SomeItem
    {
      private string _Value;
      private int _Version = 0;
      private int _LastKnown = 0;
    
      public SomeItem()
      {
    
      }
    
      public SomeItem(string value)
      {
         this._Value = value
      }
    
    
      public string Value
      {
         get { return this._Value; }
         set { if (this._Value != value) { this._Value = value; this._Version++; } }
      }
    
      public int Version
      {
        get { return this._Version; }
      }
    
      public bool HasChanged
      {
         get { return (this._Version != _LastKnown); }
      }
    
     // Reset change tracking.
     public void Reset()
     {
        this._LastKnown = this._Version;
     }
    }
    

    Elsewhere you could use something like this in your loop.

    private List<SomeItem> _Items = new List<SomeItem>();
    
    // Add an item.
    _Items.Add(new SomeItem("ABC"));
    
    // Add a changed item.
    SomeItem itemN = new SomeItem("EFG");
    itemN.Value = "XYZ";
    _Items.Add(itemN);
    
    
    foreach (SomeItem item in _Items)
    {
       if (item.HasChanged)
       {
          // Do stuff here.
          //throw new Exception()
       }
    }
    

    UPDATED

    Alternatively if you just want to find out what items have changed you could do something like this.

    SomeItem[] changedItems = _Items.Where(item => item.HasChanged);
    

    As per dlev’s suggestion if you just want to know if any changed just use.

    if (_Items.Any(item => item.HasChanged))
    {
       // An item has changed, do stuff.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question that's so simple I cannot believe I can't answer it
I have a very simple question I believe. I have a footer.php that I
Simple question, but one that I've been curious about...is there a functional difference between
Simple question that keeps bugging me. Should I HTML encode user input right away
I believe this may be a simple question, but I've looked everywhere and tried
I believe this is a fairly simple question but I have no idea where
Simple question I believe for anyone with minimal php skills (which I don't have
Hey guys, (relatively, I believe) simple question here, I have a UITableViewController with its
I'm flummoxed with (what I believe is) a seemingly-simple theoretical question about SAP BusinessObjects
I believe my question is pretty simple so this should be a piece of

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.