Firefighter or traineefirefighter or anything else

Info icon This preview shows pages 252–254. Sign up to view the full content.

Firefighter , or TraineeFirefighter , or anything else that derives from FirefighterBase . (And each element is allowed to refer to an object of a different type, as long as the objects are all compatible with the element type.) Likewise, you can declare an array of any interface type—for example, INamedPerson[] , in which case each element can refer to any object of any type that implements that interface. Taking this to extremes, an array of type object[] has elements that can refer to any object of any reference type, or any boxed value. As you will remember from Chapter 3 , the alternative to a reference type is a value type . With value types, each variable holds its own copy of the value, rather than a reference to some potentially shared object. As you would expect, this behavior carries over to arrays when the element type is a value type. Consider the array shown in Example 7-12 . Example 7-12. An array of integer values int[] numbers = { 2, 3, 5, 7, 11 }; Like all the numeric types, int is a value type, so we end up with a rather different structure. As Figure 7-3 shows, the array elements are the values themselves, rather than references to values. Why would you need to care about where exactly the value lives? Well, there’s a sig- nificant difference in behavior. Given the numbers array in Example 7-12 , consider this code: 228 | Chapter 7: Arrays and Lists
Image of page 252

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

int thirdElementInArray = numbers[2]; thirdElementInArray += 1; Console.WriteLine("Variable: " + thirdElementInArray); Console.WriteLine("Array element: " + numbers[2]); which would print out the following: Variable: 6 Array element: 5 Figure 7-3. An array with value type elements Because we are dealing with a value type, the thirdElementInArray local variable gets a copy of the value in the array. This means that the code can change the local variable without altering the element in the array. Compare that with similar code working on the array from Example 7-10 : CalendarEvent thirdElementInArray = events[2]; thirdElementInArray.Title = "Modified title"; Console.WriteLine("Variable: " + thirdElementInArray.Title); Console.WriteLine("Array element: " + events[2].Title); This would print out the following: Variable: Modified title Array element: Modified title This shows that we’ve modified the event’s title both from the point of view of the local variable and from the point of view of the array element. That’s because both refer to the same CalendarEvent object—with a reference type, when the first line gets an ele- ment from the array we don’t get a copy of the object, we get a copy of the reference to that object. The object itself is not copied. The distinction between the reference and the object being referred to means that there’s sometimes scope for ambiguity—what exactly does it mean to change an ele- ment in an array? For value types, there’s no ambiguity, because the element is the value. The only way to change an entry in the numbers array in Example 7-12 is to assign a new value into an element: numbers[2] = 42; Arrays | 229
Image of page 253
Image of page 254
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