However all the finding and sorting functionality we

Info icon This preview shows pages 278–280. Sign up to view the full content.

. However, all the finding and sorting functionality we saw earlier is still available; you just have to use the methods provided by List<T> instead of Array : DateTime dateOfInterest = new DateTime(2009, 7, 12); List<CalendarEvent> itemsOnDateOfInterest = events.FindAll( e => e.StartTime.Date == dateOfInterest); Notice a slight stylistic difference—whereas with arrays, FindAll is a static method provided by the Array class, List<T> chooses to make its FindAll method an instance member—so we invoke it as events.FindAll . Style aside, it works in exactly the same way. As you might expect, it returns its results as another List<T> rather than as an array. This same stylistic difference exists with all the other techniques we looked at before. List<T> provides Find , FindLast , FindIndex , FindLastIndex , IndexOf , LastIndexOf , and Sort methods that all work in almost exactly the same way as the array equivalents we looked at earlier, but again, they’re instance methods rather than static methods. Since List<T> offers almost everything you’re likely to want from an array and more besides, List<T> will usually be your first choice to represent a collection of data. (The only common exception is if you need a rectangular array.) Unfortunately, you will sometimes come up against APIs that simply require you to provide an array. In fact, we already wrote some code that does this: the AddNumbers method back in Exam- ple 7-3 requires its input to be in the form of an array. But even this is easy to deal with: List<T> provides a handy ToArray() method for just this eventuality, building a copy of the list’s contents in array form. But wouldn’t it be better if we could write our code in such a way that it didn’t care whether incoming information was in an array, a List<T> , or some other kind of col- lection? It is possible to do exactly this, using the polymorphism techniques discussed in Chapter 4 . Collections and Polymorphism Polymorphic code is code that is able to work on a variety of different forms of data. The foreach keyword has this characteristic. For example: foreach (CalendarEvent ev in events) { Console.WriteLine(ev.Title); } This code works if events is an array— CalendarEvent[] —but it works equally well if events is a List<CalendarEvent> . And in fact, there are many more specialized collection types in the .NET Framework class library that we’ll look at in a later chapter that foreach can work with. You can even arrange for it to work with custom collection classes you may have written yourself. All this is possible because the .NET Framework 254 | Chapter 7: Arrays and Lists
Image of page 278

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

defines some standard interfaces for representing collections of things. The foreach construct depends on a pair of interfaces: IEnumerable<T> and IEnumerator<T> . These derive from a couple of nongeneric base interfaces, IEnumerable and IEnumerator . These interfaces are defined in the class library, and they are reproduced in Example 7-29 .
Image of page 279
Image of page 280
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ 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