Example 7 29 enumeration interfaces namespace public

Info icon This preview shows pages 279–281. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 7-29. Enumeration interfaces namespace System.Collections.Generic { public interface IEnumerable<out T> : IEnumerable { new IEnumerator<T> GetEnumerator(); } public interface IEnumerator<out T> : IDisposable, IEnumerator { new T Current { get; } } } namespace System.Collections { public interface IEnumerable { IEnumerator GetEnumerator(); } public interface IEnumerator { bool MoveNext(); object Current { get; } void Reset(); } } The split between the generic and nongeneric interfaces here is a historical artifact. Versions 1.0 and 1.1 of .NET did not support generics, so only the base IEnumerable and IEnumerator interfaces existed. When .NET 2.0 shipped in 2005, generics were introduced, making it possible to provide versions of these interfaces that were explicit about what type of objects a collection contains, but in order to maintain backward compatibility the old version 1.x interfaces had to remain. You will normally use the generic versions, because they are easier to work with. Conceptually, if a type implements IEnumerable<T> it is declaring that it contains a sequence of items of type T . To get hold of the items, you can call the GetEnumerator method, which will return an IEnumerator<T> . An enumerator is an object that lets you work through the objects in an enumerable collection one at a time. The split between enumerables and enumerators makes it possible to have different parts of your program ‡ If you’re familiar with C++ and its Standard Template Library, an enumerator is broadly similar in concept to an iterator in the STL. Collections and Polymorphism | 255
Image of page 279

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
working their way through the same collection at the same time, without all of them needing to be in the same place. This can be useful in multithreaded applications (al- though as we’ll see in a later chapter, you have to be extremely careful about letting multiple threads use the same data structure simultaneously). Some enumerable collections, such as List<T> , can be modified. (.NET defines an IList<T> interface to represent the abstract idea of a modifi- able, ordered collection. List<T> is just one implementation IList<T> .) You should avoid modifying a collection while you’re in the process of iterating through it. For example, do not call Add on a List<T> in the middle of a foreach loop that uses that list. List<T> detects when this happens, and throws an exception. Note that unlike IList<T> , IEnumerable<T> does not provide any meth- ods for modifying the sequence. While this provides less flexibility to the consumer of a sequence, it broadens the range of data that can be wrapped as an IEnumerable<T> . For some sources of data it doesn’t make sense to provide consumers of that data with the ability to reorder it. These interfaces make it possible to write a function that uses a collection without having any idea of the collection’s real type—you only need to know what type of elements it contains. We could rewrite Example 7-3 so that it works with any IEnumer able<string> rather than just an array of strings, as shown in Example 7-30 .
Image of page 280
Image of page 281
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern