224 chapter 7 arrays and lists array arguments and

Info icon This preview shows pages 248–251. Sign up to view the full content.

View Full Document Right Arrow Icon
224 | Chapter 7: Arrays and Lists
Image of page 248

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

View Full Document Right Arrow Icon
Array arguments and the params keyword The String.Split method breaks a string into multiple strings based on separator characters. You tell it which characters to treat as separators by passing a char array. Example 7-8 splits on spaces, commas, and periods. Example 7-8. Array arguments in the class library string[] items = inputString.Split( new char[] { ' ', ',', '.' }, StringSplitOptions.RemoveEmptyEntries); If inputString contained "One,Two Three, Four. Five." , this would put a five-element array into items containing the strings "One" , "Two" , "Three" , "Four" , and "Five" . Example 7-8 asks Split to ignore empty items so that when we get both a period and a space in succession we don’t get an empty string in the results to represent the fact that there were two separators. If you don’t need to skip such things, there’s a simpler overload of Split that illustrates yet another way to initialize an array: string[] items = inputString.Split(' ', ',', '.'); It looks like we’ve passed three char arguments to this method. But there’s no such overload of Split —this ends up calling an overload that looks like this: public string[] Split(params char[] separator) ... That params keyword is significant. When an argument is marked with this keyword, C# lets you use syntax that makes it look like a series of individual arguments, and it will create an array from these for you. (You’re free to provide the array explicitly if you prefer.) The params keyword can be used on only the very last argument of a method, to avoid potential ambiguity about which values go into arrays and which become arguments in their own right. That’s why Example 7-8 had to create the array explicitly. The examples so far contain nothing but strings. This is a poor way to represent events in a calendar—it would be useful to know when each event occurs. We could add a second array of type DateTimeOffset[] whose elements correspond to the event names in the original array. But spreading related data across multiple arrays can make code awkward to write and hard to maintain. Fortunately, there’s a better way. Custom Types in Arrays You can create an array using any type for the element type—you’re not limited to types provided by the .NET Framework class library. You can use a class defined in the way shown in Chapter 3 , such as the calendar event type in Example 7-9 . Example 7-9. Custom class to represent events in a calendar class CalendarEvent { public string Title { get; set; } Arrays | 225
Image of page 249
public DateTimeOffset StartTime { get; set; } public TimeSpan Duration { get; set; } } This class holds the event’s title, start time, and duration in a single object. We can create an array of these objects, as shown in Example 7-10 . Example 7-10. Creating an array with a custom element type CalendarEvent[] events = { new CalendarEvent { Title = "Swing Dancing at the South Bank", StartTime = new DateTimeOffset (2009, 7, 11, 15, 00, 00, TimeSpan.Zero), Duration = TimeSpan.FromHours(4) }, new CalendarEvent { Title = "Saturday Night Swing", StartTime = new DateTimeOffset (2009, 7, 11, 19, 30, 00, TimeSpan.Zero), Duration = TimeSpan.FromHours(6.5) },
Image of page 250

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

View Full Document Right Arrow Icon
Image of page 251
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