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.

98 CHAPTER 3 Parameterized typ<strong>in</strong>g with genericsList<strong>in</strong>g 3.13 starts off by just creat<strong>in</strong>g a list of all the <strong>in</strong>tegers between 2 and 100 <strong>in</strong>clusiveB—noth<strong>in</strong>g spectacular here, although once aga<strong>in</strong> I should po<strong>in</strong>t out thatthere’s no box<strong>in</strong>g <strong>in</strong>volved. The delegate used <strong>in</strong> step C is a Predicate , andthe one used <strong>in</strong> D is an Action. One po<strong>in</strong>t to note is how simple the use ofRemoveAll is. Because you can’t change the contents of a collection while iterat<strong>in</strong>gover it, the typical ways of remov<strong>in</strong>g multiple elements from a list have previously beenas follows:■ Iterate us<strong>in</strong>g the <strong>in</strong>dex <strong>in</strong> ascend<strong>in</strong>g order, decrement<strong>in</strong>g the <strong>in</strong>dex variablewhenever you remove an element.■ Iterate us<strong>in</strong>g the <strong>in</strong>dex <strong>in</strong> descend<strong>in</strong>g order to avoid excessive copy<strong>in</strong>g.■ Create a new list of the elements to remove, and then iterate through the newlist, remov<strong>in</strong>g each element <strong>in</strong> turn from the old list.None of these is particularly satisfactory—the predicate approach is much neater, giv<strong>in</strong>gemphasis to what you want to achieve rather than how exactly it should happen. It’s agood idea to experiment with predicates a bit to get comfortable with them, particularlyif you’re likely to be us<strong>in</strong>g <strong>C#</strong> 3 <strong>in</strong> a production sett<strong>in</strong>g any time <strong>in</strong> the near future—thismore functional style of cod<strong>in</strong>g is go<strong>in</strong>g to be <strong>in</strong>creas<strong>in</strong>gly important over time.Next we’ll have a brief look at the methods that are present <strong>in</strong> ArrayList but notList, and consider why that might be the case.FEATURES “MISSING” FROM LISTA few methods <strong>in</strong> ArrayList have been shifted around a little—the static ReadOnlymethod is replaced by the AsReadOnly <strong>in</strong>stance method, and TrimToSize is nearlyreplaced by TrimExcess (the difference is that TrimExcess won’t do anyth<strong>in</strong>g if thesize and capacity are nearly the same anyway). There are a few genu<strong>in</strong>ely “miss<strong>in</strong>g”pieces of functionality, however. These are listed, along with the suggestedworkaround, <strong>in</strong> table 3.3.Table 3.3Methods from ArrayList with no direct equivalent <strong>in</strong> ListArrayList methodWay of achiev<strong>in</strong>g similar effectAdapterCloneFixedSizeRepeatSetRangeSynchronizedNone providedlist.GetRange (0, list.Count) or new List(list)Nonefor loop or write a replacement generic methodfor loop or write a replacement generic methodSynchronizedCollectionThe Synchronized method was a bad idea <strong>in</strong> ArrayList to start with, <strong>in</strong> my view. Mak<strong>in</strong>g<strong>in</strong>dividual calls to a collection doesn’t make the collection thread-safe, because somany operations (the most common is iterat<strong>in</strong>g over the collection) <strong>in</strong>volve multipleLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!