You may be wondering why net offers arrays when listt

Info icon This preview shows pages 268–270. Sign up to view the full content.

View Full Document Right Arrow Icon
You may be wondering why .NET offers arrays when List<T> appears to be more useful. The answer is that it wouldn’t be possible for List<T> to exist if there were no arrays: List<T> uses an array internally to hold its elements. As you add elements, it allocates new, larger arrays as necessary, copying the old contents over. It employs various tricks to minimize how often it needs to do this. 244 | Chapter 7: Arrays and Lists
Image of page 268

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

View Full Document Right Arrow Icon
List<T> is one of the most useful types in the .NET Framework. If you’re dealing with multiple pieces of information, as programs often do, it’s very common to need some flexibility around the amount of information—fixed-size lists are the exception rather than the rule. (An individual’s calendar tends to change over time, for example.) So have we just wasted your time with the first half of this chapter? Not at all—not only do arrays crop up a lot in APIs, but List<T> collections are very similar in use to arrays. We could migrate most of the examples seen so far in this chapter from arrays to lists. Returning to our earlier, nonrectangular example, we would need to modify only the first line of Example 7-10 , which creates an array of CalendarEvent objects. That line currently reads: CalendarEvent[] events = It is followed by the list of objects to add to the array, contained within a pair of braces. If you change that line to this: List<CalendarEvent> events = new List<CalendarEvent> the initializer list can remain the same. Notice that besides changing the variable dec- laration to use the List<T> type (with the generic type argument T set to the element type CalendarEvent , of course) we also need an explicit call to the constructor. (Nor- mally, you’d expect parentheses after the type name when invoking a constructor, but those are optional when using an initializer list.) As you saw earlier, the use of new is optional when assigning a value to a newly declared array, but C# does not extend that courtesy to other collection types. While we can initialize the list in much the same way as we would an array, the differ- ence is that we are free to add and remove elements later. To add a new element, we can use the Add method: CalendarEvent newEvent = new CalendarEvent { Title = "Dean Collins Shim Sham Lesson", StartTime = new DateTimeOffset (2009, 7, 14, 19, 15, 00, TimeSpan.Zero), Duration = TimeSpan.FromHours(1) }; events.Add(newEvent); This appends the element to the end of the list. If you want to put the new element somewhere other than at the end, you can use Insert : events.Insert(2, newEvent); The first argument indicates the index at which you’d like the new item to appear— any items at or after this index will be moved down to make space. You can also remove items, either by index, using the RemoveAt method, or by passing the value you’d like to remove to the Remove method (which will remove the first element it finds that con- tains the specified value).
Image of page 269
Image of page 270
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