Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Exceptions related to IEnumerator

Exceptions related to IEnumerator

Scheduled Pinned Locked Moved C#
tutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SeeBees
    wrote on last edited by
    #1

    According to MSDN, methods of IEnumerator are expected to throw an exception if the enumerated collection has been modified For Example, List<int> list = new List<int>(); list.Add(1); IEnumerator enumerator = list.GetEnumerator(); enumerator.Reset(); // It' ok to reset the enumerator list.Add(2); // The collection is modified. enumerator.Reset(); // Now InvalidOperationException is raised When implementing the methods of IEnumerator in our own class, we should have them throw the exception if the collection is modified. Now I have a class : class MyEnumerator:IEnumerator{ int[] integers; /// /// Methods /// public void Reset(){ } } How to implement Reset in an elegant way, so that I can throw an InvalidOperationException when Reset is called after integers are modified? Thank you! :-O

    L 1 Reply Last reply
    0
    • S SeeBees

      According to MSDN, methods of IEnumerator are expected to throw an exception if the enumerated collection has been modified For Example, List<int> list = new List<int>(); list.Add(1); IEnumerator enumerator = list.GetEnumerator(); enumerator.Reset(); // It' ok to reset the enumerator list.Add(2); // The collection is modified. enumerator.Reset(); // Now InvalidOperationException is raised When implementing the methods of IEnumerator in our own class, we should have them throw the exception if the collection is modified. Now I have a class : class MyEnumerator:IEnumerator{ int[] integers; /// /// Methods /// public void Reset(){ } } How to implement Reset in an elegant way, so that I can throw an InvalidOperationException when Reset is called after integers are modified? Thank you! :-O

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I have not done this yet, but here is an idea: - in the object you want to be able to enumerate, keep a generation counter that starts at zero and gets incremented every time you modify the collection (hence Add, Remove, Clear, ... all contain "generation++;") - in the enumerator hold a copy of the generation counter, equal to the collection's generation at the moment you create the enumerator - in each enumerator method compare enumerator generation with collection generation; when unequal, throw the exception. Refinement: to reduce the risk of generation overflow, rather than incrementing all the time, your collection could keep two generation values (current and next), that both start at zero. Each modification sets "generation=nextGeneration;", and each getEnumerator does "generation=nextGeneration++;" hence the generation can only increase if an enumerator has been requested. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


      S 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, I have not done this yet, but here is an idea: - in the object you want to be able to enumerate, keep a generation counter that starts at zero and gets incremented every time you modify the collection (hence Add, Remove, Clear, ... all contain "generation++;") - in the enumerator hold a copy of the generation counter, equal to the collection's generation at the moment you create the enumerator - in each enumerator method compare enumerator generation with collection generation; when unequal, throw the exception. Refinement: to reduce the risk of generation overflow, rather than incrementing all the time, your collection could keep two generation values (current and next), that both start at zero. Each modification sets "generation=nextGeneration;", and each getEnumerator does "generation=nextGeneration++;" hence the generation can only increase if an enumerator has been requested. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


        S Offline
        S Offline
        SeeBees
        wrote on last edited by
        #3

        Yeah, I see, thanks for your neat idea. Then my collection class has to expose an extra property, say Generation{get{}}, right?

        L 1 Reply Last reply
        0
        • S SeeBees

          Yeah, I see, thanks for your neat idea. Then my collection class has to expose an extra property, say Generation{get{}}, right?

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          SeeBees wrote:

          expose an extra property, say Generation{get{}},

          Not really, if your Collection inherits from an existing one, you can implement your own public override IEnumerator GetEnumerator() just like the original collection did. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups