locked
What exactly does IEnumerable<T> give you? RRS feed

  • Question

  • I'm learning parts of C# that work well with MVC.

    I generally understand that IEnumerable<T> provides a mechanism to iterate, but I'm curious to know what it actually is in memory: is it a reference, something like a pointer to an object array?

    Wednesday, August 16, 2017 4:11 PM

Answers

  • Hi Coreysan,

    As we know, IEnumerable<T>  is a generic interface. You could use it as follows. Because IEnumerable<T> is the base interface for generic collections that can be enumerated.

    IEnumerable<string> ienums = new List<string> { "123","456"};//instance of all collection classes

    IEnumerable(Of T) is a huge step up in the 2.0 framework from the original IEnumerable interface.  It provides a typed enumeration which eliminates lots of nasty casts.  The best part is that IEnumerable(Of T) is backwards compatible with IEnumerable (it inherits from it). IEnumerable can guarantee the security of the data, realize the code reused. Since IEnumerable(Of Object) and IEnumerable have virtually the same, it's easy to create a wrapper class that forwards the calls into the base enumerator.

    You could refer to the following link which explains IEnumerable interface.

    https://msdn.microsoft.com/en-us/library/system.collections.ienumerable(v=vs.110).aspx

    If the issue has been solved , I hope you could mark the reply as answer because this will help others looking for the same or similar issues down the road.
                                                       
    Best Regards,

    feih_7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei Hu Friday, August 18, 2017 7:03 AM
    • Marked as answer by Coreysan Friday, September 1, 2017 6:51 PM
    Friday, August 18, 2017 7:02 AM

All replies

  • IEnumerable is an interface so each implementation decides what it needs and doesn't. For example you can implement the interface without ever actually creating a type for it (ignoring what the compiler generates under the hood).

    public static class SomeClass
    {
       public static IEnumerable<int> ReturnIntegers ()
       {
          yield return 1;
          yield return 2;
          yield return 3;
       }
    }

    In many cases though the enumerator is just a private class instance that wraps the list/collection being enumerated. Beyond the reference to the original list/collection and perhaps a current position field there is no more data needed.

    Michael Taylor
    http://www.michaeltaylorp3.net

     

    • Proposed as answer by Fei Hu Tuesday, August 22, 2017 8:09 AM
    Wednesday, August 16, 2017 5:22 PM
  • Thanks Michael - I look forward to reading through your website.!
    Wednesday, August 16, 2017 11:23 PM
  • Thanks - this was perfect!
    Wednesday, August 16, 2017 11:55 PM
  • Hi Coreysan,

    As we know, IEnumerable<T>  is a generic interface. You could use it as follows. Because IEnumerable<T> is the base interface for generic collections that can be enumerated.

    IEnumerable<string> ienums = new List<string> { "123","456"};//instance of all collection classes

    IEnumerable(Of T) is a huge step up in the 2.0 framework from the original IEnumerable interface.  It provides a typed enumeration which eliminates lots of nasty casts.  The best part is that IEnumerable(Of T) is backwards compatible with IEnumerable (it inherits from it). IEnumerable can guarantee the security of the data, realize the code reused. Since IEnumerable(Of Object) and IEnumerable have virtually the same, it's easy to create a wrapper class that forwards the calls into the base enumerator.

    You could refer to the following link which explains IEnumerable interface.

    https://msdn.microsoft.com/en-us/library/system.collections.ienumerable(v=vs.110).aspx

    If the issue has been solved , I hope you could mark the reply as answer because this will help others looking for the same or similar issues down the road.
                                                       
    Best Regards,

    feih_7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei Hu Friday, August 18, 2017 7:03 AM
    • Marked as answer by Coreysan Friday, September 1, 2017 6:51 PM
    Friday, August 18, 2017 7:02 AM