CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    foreach and IEnumerator

    Hello everyone,


    Two questions,

    1.

    How foreach utilizes the IEnumerator interface and IEnumerable interface? Any there any docuements? I am interested in it.

    2.

    Pros and cons compared with using foreach and using simple index variable to iterate? Like,

    Code:
    for (int i = 0; i < abc.Count; i++)
    {
        // access abc [i] here
    }

    thanks in advance,
    George

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: foreach and IEnumerator

    Use Lutz's .Net Reflector to view the original source code. Search on google to find the link to this free tool.

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: foreach and IEnumerator

    Thanks Arjay,


    I have found some code, share here.

    http://www.csharpfriends.com/Spec/in...cID=15.8.4.htm

    I read the document. There are two expansions for foreach, but I can not distinguish the differences, what are the differences?

    --------------------
    2 If the collection expression is of a type that implements the collection pattern (as defined above), the expansion of the foreach statement is:

    3 Otherwise; the collection expression is of a type that implements System.IEnumerable, and the expansion of the foreach statement is:
    --------------------


    Quote Originally Posted by Arjay
    Use Lutz's .Net Reflector to view the original source code. Search on google to find the link to this free tool.

    regards,
    George

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: foreach and IEnumerator

    It explains it here:

    1 A type C is said to be a collection type if it implements the System.IEnumerable interface or implements the collection pattern by meeting all of the following criteria:

    2 C contains a public instance method with the signature GetEnumerator(), that returns a struct-type, class-type, or interface-type, which is called E in the following text.
    3 E contains a public instance method with the signature MoveNext() and the return type bool.
    4 E contains a public instance property named Current that permits reading the current value. 5 The type of this property is said to be the element type of the collection type.
    Take the above information and compare it to how IEnumerable is defined and then you will know the differences.

    Say, when you get a chance can you post some code of projects you are working on?

  5. #5
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: foreach and IEnumerator

    Thanks Arjay,


    But I think in both cases, the collector class needs to implement IEnumerator interface, since method GetEnumerator is from this interface and it is used in both cases, right?

    Quote Originally Posted by Arjay
    It explains it here:



    Take the above information and compare it to how IEnumerable is defined and then you will know the differences.

    Say, when you get a chance can you post some code of projects you are working on?

    regards,
    George

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: foreach and IEnumerator

    Quote Originally Posted by George2
    I read the document. There are two expansions for foreach, but I can not distinguish the differences, what are the differences?
    The difference is very trivial. You only need to follow the text and compare the patterns.
    In the case where the collection pattern is implemented directly you will have a GetEnumerator() method as part of the collection pattern so you can use it.
    If you havn't you have to implement it using IEnumerable and therefore the GetEnumerator() is implemented by the Interface System.IEnumerable
    Thats all about the differences. But I'm back to the basics.: DO EXAMPLES. Do lots of examples. If you are trying to study this language dont only use the ECMA specifications. They are great, but as they have to be very general they can confuse you. Practice is the point. Where to learn a language: Best coming to the contry where it is spoken. Why ? The daily practice. Whats the badest way to learn a language: Reading the dictionary only. I dont say. cannot be done - no but this way is full of stones in the way and heavy rocks to climb.
    If you have done half a gigabyte of examples and in between reading books then I'm sure you will be able to do it.
    I have seen you said: I first need to know this... bla, bla.. before I do the example. Wrong pattern. During learning Try and error is totally ok.
    Its the way babys learn to go. They do their first step and fall down. stand up and do the next and so on. Thats try and error. If you want to spare time by not doing the examples and to first find out theoretically -- you will not do it.-- You get no praxis. Stop your behaviour to be a 'Theoretical' one.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: foreach and IEnumerator

    Quote Originally Posted by George2
    But I think in both cases, the collector class needs to implement IEnumerator interface, since method GetEnumerator is from this interface and it is used in both cases, right?
    TRY IT, simple Try it, my dear. take a class and implement a method GetEnumerator without implementing the Interface and try if it works with foreach. that answers your question.

    You are figuring around nothing else
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  8. #8
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: foreach and IEnumerator

    Thanks for your advice, JonnyPoet!


    I have made some exercise and answered this question by myself. The last question, how could I see the expand statements (e.g. how C# compiler expand foreach) in Visual Studio?

    Quote Originally Posted by JonnyPoet
    TRY IT, simple Try it, my dear. take a class and implement a method GetEnumerator without implementing the Interface and try if it works with foreach. that answers your question.

    You are figuring around nothing else

    regards,
    George

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured