locked
IEnumerable RRS feed

  • General discussion


  • I always implemented an IEnumerable object like this:

    -----------------------

       Class CollectionItem
        .
        .
        .
       End Class


       Class Collection : Implements IEnumerable
          Private InternalCollectionContent as Whatever(of CollectionItem)
            .
            .
            .
          Private Class CollectionEnumerator : Implements IEnumerator
             Private InternalCollectionContent as Whatever(of CollectionItem)
               .
               .
               .
          End Class
       End Class

    ------------------------

    And it works just perfect !


    But I noticed that on MSDN there is an exemple that is made to demonstrate how
    to do the implementation. It goes like this:
     
    -----------------------

       Public Class CollectionItem
          .
          .
          .
       End Class


       Public Class Collection : Implements IEnumerable
          Private InternalCollectionContent as Whatever(of CollectionItem)
            .
            .
            .
       End Class

       Public Class CollectionEnumerator : Implements IEnumerator
          Public InternalCollectionContent as Whatever(of CollectionItem)
             .
             .
             .
       End Class

    -----------------------

    I do not understand why the Microsoft implementation method expose the IEnumerator class
    as Public and I do not understand neither why the the internal content of the collection
    is exposed (Public) from the IEnumerator class

    What is the reason for this

    Tuesday, December 1, 2009 1:34 AM

All replies

  • CrazyPenny,

    If you ask yourself why Microsoft did something, than start asking yourself why your collegue did something.

    Not everything has a reason or is just because somebody forgot something.

    Does it harm for you and where?

    All those things are done by simple developers, they too do things they latter on don't understand why they did it like that, like I do as well.


    Cor
    Tuesday, December 1, 2009 7:30 AM

  • When I read the code of somebody that is a better developer than I am, I always look at how he does thing. This is a very good source of knowledge.


    But here you are right, MSDN code don't even compile. The compiler will not let you expose the content of a private variable from a public reference to it in an another class

    ------
    you asked, Does it harm me how they did something ?

    If I cant trust the way Microsoft tells you how to implement an interface, what good is this library for a developer?
    Tuesday, December 1, 2009 11:55 AM

  • Crazypennie,

    Good call on finding a bug in the library. 
    Go to the page again, and use the "Report Bug" link.

    As far as best coding practices in the general samples, there are disclaimers scattered around the library that tell you samples do not conform to the best or safest coding practices.  I understand and agree with this position.  The *intent* of the examples is demonstrate how a given type or method is designed to be used, and not confuse the learner with 'best practice' coding patterns.  There is a separate section of the library dedicated to that topic.

    Rudy   =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, December 1, 2009 3:02 PM
  • Hi Crazypennie,

    Which example are you talking about? It would be good if you could provide the link to it.

    Private modifier are  meant to be defined inside another type, not  as a independant  type itself.

    but not sure what you meant.

    Thanks anyway


    Arjun Paudel
    Tuesday, December 1, 2009 4:16 PM


  • Arjun

    I am talking about this

    http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx


    If you refer to my first post, You will see how I use to implement the IEnumerator class inside the IEnumerable one.

    I Use to do this so the enumerator and the intern content of the collection never get exposed.

    I will agree with you if you say that the reference to the enumerator exist only in the GetEnumerator function and is not anyway accessible to the rest of the application and the internal data stay innacessible. Still, the class itself is accessible. To me the class has no reason to be exposed.

    Also, the way it is showed in the exemple, the Ienumerator class using a public variable to hold the private content of the Ienumerable class, this is not something that the compiler will accept


    So there is a bug in the exemple, but now this does not tells me if my way to implement this is good, regardless to the fact that it works good.

    We have to considere the fact that the Ienumerator class is mainly there for some "Under The Hood" processing.
    Tuesday, December 1, 2009 5:30 PM
  • I agree with you I think. I had a brief look at that, and thought I dont need IEnumerator to be public unless I am going to extend it and as long as I am not going to use values of collection in nested class. Yes public member in that example is little strange too.

    Even implementing both in collection(Implements IEnumerable, IEnumerator) is possible, as long as you are not going to iterate same collection in loop(binary tree, linked list etc) and not going to use multithread in such collection, but I suggest to avoid that one strictly.

    There must be other reasons as well to create 2 interfaces to do this task but I can think as above only at the moment. And also I think there were some problems going this way before .net 2.0, but now better. Since the example is still valid for framework version prior to 2.0,  I think the example still remains the same, though it could be made more specific to each framework version.

    Arjun Paudel
    Wednesday, December 2, 2009 2:25 PM


  • Any way, If somebody see some info on this in the future, I would be interrested to know.
    Wednesday, December 2, 2009 11:21 PM