Because the c compiler knows what an array is and is

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

because the C# compiler knows what an array is, and is able to generate code that deals directly with array elements in situ . But there’s no way to write a custom indexer that offers the same flexibility. (There are reasons for this, but to explain them would require an exploration of the .NET Framework’s type safety rules, which would be lengthy and quite outside the scope of this chapter.) Having established the root of the problem, let’s look at what this means for List<T> . Immutability and List<T> The List<T> class gets no special privileges—it may be part of the .NET Framework class library, but it is subject to the same restrictions as your code. And so it has the same problem just described—the following code will produce the same compiler error you saw in the preceding section: List<CanChange> numbers = new List<CanChange> { new CanChange() }; numbers[0].Number = 42; // Will not compile One way of dealing with this would be to avoid using custom value types in a collection class such as List<T> , preferring custom reference types instead. And that’s not a bad rule of thumb—reference types are a reasonable default choice for most data types. However, value types do offer one compelling feature if you happen to be dealing with very large volumes of data. As Figure 7-1 showed earlier, an array with reference type elements results in an object for the array itself, and one object for each element in the array. But when an array has value type elements, you end up with just one object— the values live inside the array, as Figure 7-3 illustrates. List<T> has similar character- istics because it uses an array internally. List<T> | 251
Image of page 275

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

For an array with hundreds of thousands of elements, the simpler structure of Figure 7-3 can have a noticeable impact on performance. For example, I just ran a quick test on my computer to see how long it would take to create a List<CanChange> with 500,000 entries, and then run through the list, adding the Number values together. Example 7-28 shows the code—it uses the Stopwatch class from the System.Diagnos tics namespace, which provides a handy way to see how long things are taking. Example 7-28. Microbenchmarking values versus references in lists Stopwatch sw = new Stopwatch(); sw.Start(); int itemCount = 500000; List<CanChange> items = new List<CanChange>(itemCount); for (int i = 0; i < itemCount; ++i) { items.Add(new CanChange { Number = i }); } sw.Stop(); Console.WriteLine("Creation: " + sw.ElapsedTicks); sw.Reset(); sw.Start(); int total = 0; for (int i = 0; i < itemCount; ++i) { total += items[i].Number; } sw.Stop(); Console.WriteLine("Total: " + total); Console.WriteLine("Sum: " + sw.ElapsedTicks); With CanChange as a value type, it takes about 150 ms on my machine to populate the list, and then about 40 ms to run through all the numbers, adding them together. But if I change CanChange from a struct to a class (i.e., make it a reference type) the numbers become more like 600 ms and 50 ms, respectively. So that’s about 25 percent longer to perform the calculations but a staggering four times longer to create the collection in the first place. And that’s because with CanChange
Image of page 276
Image of page 277
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