New calendarevent title formula 1 german grand prix

Info icon This preview shows pages 250–253. Sign up to view the full content.

View Full Document Right Arrow Icon
new CalendarEvent { Title = "Formula 1 German Grand Prix", StartTime = new DateTimeOffset (2009, 7, 12, 12, 10, 00, TimeSpan.Zero), Duration = TimeSpan.FromHours(3) }, new CalendarEvent { Title = "Swing Dance Picnic", StartTime = new DateTimeOffset (2009, 7, 12, 15, 00, 00, TimeSpan.Zero), Duration = TimeSpan.FromHours(4) }, new CalendarEvent { Title = "Stompin' at the 100 Club", StartTime = new DateTimeOffset (2009, 7, 13, 19, 45, 00, TimeSpan.Zero), Duration = TimeSpan.FromHours(5) } }; Notice that Example 7-10 uses the new keyword to initialize each object. This highlights an important point about arrays: individual array elements are similar to variables of the same type. Recall from Chapter 3 that a custom type defined with the class key- word, such as the CalendarEvent type in Example 7-9 , is a reference type . This means that when you declare a variable of that type, the variable does not represent a particular object—it’s a storage location that can refer to an object. And the same is true of each element in an array if the element type is a reference type. Figure 7-1 shows the objects that Example 7-10 creates: five CalendarEvent objects (shown on the right), and an array 226 | Chapter 7: Arrays and Lists
Image of page 250

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

View Full Document Right Arrow Icon
object of type CalendarEvent[] (shown on the left) where each element in the array refers to one of the event objects. Figure 7-1. An array with reference type elements As you saw in Chapter 3 , with reference types multiple different variables can all refer to the same object. Since elements in an array behave in a similar way to local variables of the element type, we could create an array where all the elements refer to the same object, as shown in Example 7-11 . Example 7-11. Multiple elements referring to the same object CalendarEvent theOnlyEvent = new CalendarEvent { Title = "Swing Dancing at the South Bank", StartTime = new DateTimeOffset (2009, 7, 11, 15, 00, 00, TimeSpan.Zero), Duration = TimeSpan.FromHours(4) }; CalendarEvent[] events = { theOnlyEvent, theOnlyEvent, theOnlyEvent, theOnlyEvent, theOnlyEvent }; Figure 7-2 illustrates the result. While this particular example is not brilliantly useful, in some situations it’s helpful for multiple elements to refer to one object. For example, imagine a feature for booking meeting rooms or other shared facilities—this could be a useful addition to a calendar program. An array might describe how the room will be used today, where each element represents a one-hour slot for a particular room. If Arrays | 227
Image of page 251
the same individual had booked the same room for two different slots, the two corre- sponding array elements would both refer to the same person. Figure 7-2. An array where all of the elements refer to the same object Another feature that reference type array elements have in common with reference type variables and arguments is support for polymorphism. As you saw in Chapter 4 , a variable declared as some particular reference type can refer to any object of that type, or of any type derived from the variable’s declared type. This works for arrays too— using the examples from Chapter 4 , if an array’s type is FirefighterBase[] , each ele- ment could refer to a
Image of page 252

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

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