IEnumerable<T> and IEnumerator - some clarification please

ManxJason

Good afternoon,

I'm currently reading the Pro ASP.net MVC 4 book published by Apress, and Im on the section which talks about extension methods.

I'm struggling to understand a section of code as follows:

 public class ShoppingCart : IEnumerable<Product>
{
    public List<Product> Products { get; set; }

    public IEnumerator<Product> GetEnumerator()
    {
        return Products.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

I understand that the ShoppingCart class inherits a product IEnumerable. I understand that we have created a method that returns an Enumerator of Product. I do not understand the final method, IEnumerator IEnumerable.GetEnumerator(). Can someone please enlighten me in 'newbie' terms?

Thanks

Dmitry Bychenko

The last method you have to implement

  IEnumerator IEnumerable.GetEnumerator()

is a heritage of ancient .Net versions when .Net doesn't support generics (and IEnumerator<T>). Since then, every IEnumerable<T> for compability reasons should not only implement

  IEnumerator<T> GetEnumerator()

But old style non-generic

  IEnumerator GetEnumerator()

as well. You can see it just from the declaration

 public interface IEnumerable<out T> : IEnumerable {...}

http://msdn.microsoft.com/en-us/library/9eekhta0(v=vs.110).aspx

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

IEnumerable<T> and IEnumerator - some clarification please

From Dev

Why is IEnumerable<T> necessary when there is IEnumerator<T>?

From Dev

Initializing static class members - some clarification please

From Dev

Why doesn't the following work? (IEnumerable/ IEnumerator)

From Dev

Abstract Class - clarification please

From Dev

Abstract Class - clarification please

From Dev

Implementing IEnumerator/ IEnumerable with IDispose error

From Dev

What is the reason implementing IEnumerable and IEnumerator

From Dev

IEnumerator IEnumerable vb to C#

From Dev

IEnumerator does not implement interface IEnumerable

From Dev

LINQ merge List<IEnumerable<T>> into one IEnumerable<T> by some rule

From Dev

How does Visual Studio evaluate the IEnumerable without breaking into its IEnumerator<T>'s MoveNext?

From Dev

How does Visual Studio evaluate the IEnumerable without breaking into its IEnumerator<T>'s MoveNext?

From Dev

Need some clarification about jQuery objects that don't reside in the DOM

From Dev

MVC - the web and some clarification

From Dev

MVC - the web and some clarification

From Dev

Prototype and obj some clarification

From Dev

fitting beta distribution (in python) - clarification please

From Dev

Some clarification on numpy indexing needed?

From Dev

Need some clarification on #pragma once

From Dev

Some clarification with Java OOP theory?

From Dev

implement ienumerable with ienumerable<T>

From Dev

I need some clarification on some code samples

From Dev

LINQ IEnumerable<T[]> to IEnumerable<T>

From Dev

ienumerable to ienumerable<t> without losses

From Dev

Casting an IEnumerable to IEnumerable<T> with reflection

From Dev

Java is clearly a pass by value but wanted some clarification

From Dev

Need some clarification regarding reference types

From Dev

Need some clarification on running Cassandra nodetool repairs

Related Related

  1. 1

    IEnumerable<T> and IEnumerator - some clarification please

  2. 2

    Why is IEnumerable<T> necessary when there is IEnumerator<T>?

  3. 3

    Initializing static class members - some clarification please

  4. 4

    Why doesn't the following work? (IEnumerable/ IEnumerator)

  5. 5

    Abstract Class - clarification please

  6. 6

    Abstract Class - clarification please

  7. 7

    Implementing IEnumerator/ IEnumerable with IDispose error

  8. 8

    What is the reason implementing IEnumerable and IEnumerator

  9. 9

    IEnumerator IEnumerable vb to C#

  10. 10

    IEnumerator does not implement interface IEnumerable

  11. 11

    LINQ merge List<IEnumerable<T>> into one IEnumerable<T> by some rule

  12. 12

    How does Visual Studio evaluate the IEnumerable without breaking into its IEnumerator<T>'s MoveNext?

  13. 13

    How does Visual Studio evaluate the IEnumerable without breaking into its IEnumerator<T>'s MoveNext?

  14. 14

    Need some clarification about jQuery objects that don't reside in the DOM

  15. 15

    MVC - the web and some clarification

  16. 16

    MVC - the web and some clarification

  17. 17

    Prototype and obj some clarification

  18. 18

    fitting beta distribution (in python) - clarification please

  19. 19

    Some clarification on numpy indexing needed?

  20. 20

    Need some clarification on #pragma once

  21. 21

    Some clarification with Java OOP theory?

  22. 22

    implement ienumerable with ienumerable<T>

  23. 23

    I need some clarification on some code samples

  24. 24

    LINQ IEnumerable<T[]> to IEnumerable<T>

  25. 25

    ienumerable to ienumerable<t> without losses

  26. 26

    Casting an IEnumerable to IEnumerable<T> with reflection

  27. 27

    Java is clearly a pass by value but wanted some clarification

  28. 28

    Need some clarification regarding reference types

  29. 29

    Need some clarification on running Cassandra nodetool repairs

HotTag

Archive