Example 7 26 arrays versus indexers this classs

Info icon This preview shows pages 272–275. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 7-26. Arrays versus indexers // This class's purpose is to illustrate a difference between // arrays and indexers. Do not use this in real code! class ArrayAndIndexer<T> { public T[] TheArray = new T[100]; public T this[int index] { get { return TheArray[index]; } set { TheArray[index] = value; } } } 248 | Chapter 7: Arrays and Lists
Image of page 272

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

View Full Document Right Arrow Icon
You might think that it would make no difference whether we use this class’s indexer, or go directly for the array. And some of the time that’s true, as it is in this example: ArrayAndIndexer<int> aai = new ArrayAndIndexer<int>(); aai.TheArray[10] = 42; Console.WriteLine(aai[10]); aai[20] = 99; Console.WriteLine(aai.TheArray[20]); This swaps freely between using the array and the indexer, and as the output shows, items set through one mechanism are visible through the other: 42 99 However, things are a little different if we make this class store a mutable value type. Here’s a very simple modifiable value type: struct CanChange { public int Number { get; set; } public string Name { get; set; } } The Number and Name properties both have setters, so this is clearly not an immutable type. This might not seem like a problem—we can do more or less exactly the same with this type as we did with int just a moment ago: ArrayAndIndexer<CanChange> aai = new ArrayAndIndexer<CanChange>(); aai.TheArray[10] = new CanChange { Number = 42 }; Console.WriteLine(aai[10].Number); aai[20] = new CanChange { Number = 99, Name = "My item" }; Console.WriteLine(aai.TheArray[20].Number); That works fine. The problem arises when we try to modify a property of one of the values already inside the array. We can do it with the array: aai.TheArray[10].Number = 123; Console.WriteLine(aai.TheArray[10].Number); That works—it prints out 123 as you’d expect. But this does not work: aai[20].Number = 456; If you try this, you’ll find that the C# compiler reports the following error: error CS1612: Cannot modify the return value of 'ArrayAndIndexer<CanChange>.this[int]' because it is not a variable That’s a slightly cryptic message. But the problem becomes clear when we think about what we just asked the compiler to do. The intent of this code: aai[20].Number = 456; seems clear—we want to modify the Number property of the item whose index is 20. And remember, this line of code is using our ArrayAndIndexer<T> class’s indexer. Look- ing at Example 7-26 , which of the two accessors would you expect it to use here? Since List<T> | 249
Image of page 273
we’re modifying the value, you might expect set to be used, but a set accessor is an all or nothing proposition: calling set means you want to replace the whole element. But we’re not trying to do that here—we just want to modify the Number property of the value, leaving its Name property unmodified. If you look at the set code in Exam- ple 7-26 , it simply doesn’t offer that as an option—it will completely replace the element at the specified index in the array. The set accessor can come into play only when we’re providing a whole new value for the element, as in: aai[20] = new CanChange { Number = 456 }; That compiles, but we end up losing the Name property that the element in that location previously had, because we overwrote the entire value of the element.
Image of page 274

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

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