13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

102 CHAPTER 3 Parameterized typ<strong>in</strong>g with genericsL<strong>in</strong>ked lists make it easy to <strong>in</strong>sert another node <strong>in</strong>to the cha<strong>in</strong>—as long as youalready have a handle on the node represent<strong>in</strong>g the <strong>in</strong>sertion position. All the listneeds to do is create a new node, and make the appropriate l<strong>in</strong>ks between that nodeand the ones that will be before and after it. Lists stor<strong>in</strong>g all their data <strong>in</strong> a pla<strong>in</strong> array(as List does) need to move all the entries that will come after the new one, whichcan be very expensive—and if the array runs out of spare capacity, the whole lot mustbe copied. Enumerat<strong>in</strong>g a l<strong>in</strong>ked list from start to end is also cheap—but randomaccess (fetch<strong>in</strong>g the fifth element, then the thousandth, then the second) is slowerthan us<strong>in</strong>g an array-backed list. Indeed, L<strong>in</strong>kedList doesn’t even provide a randomaccess method or <strong>in</strong>dexer. Despite its name, it doesn’t implement IList.L<strong>in</strong>ked lists are usually more expensive <strong>in</strong> terms of memory than their array-backedcous<strong>in</strong>s due to the extra l<strong>in</strong>k node required for each value. However, they don’t havethe “wasted” space of the spare array capacity of List.The l<strong>in</strong>ked list implementation <strong>in</strong> .NET 2.0 is a relatively pla<strong>in</strong> one—it doesn’t supportcha<strong>in</strong><strong>in</strong>g two lists together to form a larger one, or splitt<strong>in</strong>g an exist<strong>in</strong>g one <strong>in</strong>totwo, for example. However, it can still be useful if you want fast <strong>in</strong>sertions at both thestart and end of the list (or <strong>in</strong> between if you keep a reference to the appropriate node),and only need to read the values from start to end, or vice versa.Our f<strong>in</strong>al ma<strong>in</strong> section of the chapter looks at some of the limitations of generics<strong>in</strong> <strong>C#</strong> and considers similar features <strong>in</strong> other languages.3.6 Limitations of generics <strong>in</strong> <strong>C#</strong> and other languagesThere is no doubt that generics contribute a great deal to <strong>C#</strong> <strong>in</strong> terms of expressiveness,type safety, and performance. The feature has been carefully designed to copewith most of the tasks that C++ programmers typically used templates for, but withoutsome of the accompany<strong>in</strong>g disadvantages. However, this is not to say limitations don’texist. There are some problems that C++ templates solve with ease but that <strong>C#</strong> genericscan’t help with. Similarly, while generics <strong>in</strong> Java are generally less powerful than <strong>in</strong><strong>C#</strong>, there are some concepts that can be expressed <strong>in</strong> Java but that don’t have a <strong>C#</strong>equivalent. This section will take you through some of the most commonly encounteredweaknesses, as well as briefly compare the <strong>C#</strong>/.NET implementation of genericswith C++ templates and Java generics.It’s important to stress that po<strong>in</strong>t<strong>in</strong>g out these snags does not imply that theyshould have been avoided <strong>in</strong> the first place. In particular, I’m <strong>in</strong> no way say<strong>in</strong>g that Icould have done a better job! The language and platform designers have had to balancepower with complexity (and the small matter of achiev<strong>in</strong>g both design andimplementation with<strong>in</strong> a reasonable timescale). It’s possible that future improvementswill either remove some of these issues or lessen their impact. Most likely, youwon’t encounter problems, and if you do, you’ll be able to work around them with theguidance given here.We’ll start with the answer to a question that almost everyone raises sooner or later:why can’t I convert a List to List?Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!