13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

96 CHAPTER 3 Parameterized typ<strong>in</strong>g with genericsThe next section is much more down to earth than our <strong>in</strong>vestigations <strong>in</strong>to reflectionand the bowels of the JIT. It covers the most common use of generics: the standardcollection classes.3.5 Generic collection classes <strong>in</strong> .NET 2.0Although this book is primarily about <strong>C#</strong> as a language, it would be foolish to ignore thefact that <strong>C#</strong> is almost always used with<strong>in</strong> the .NET Framework, and that <strong>in</strong> order to usethe language effectively you’ll need to have a certa<strong>in</strong> amount of knowledge of thelibraries too. I won’t be go<strong>in</strong>g <strong>in</strong>to the details of ADO.NET, ASP.NET, and the like, butyou’re bound to use collections <strong>in</strong> almost any .NET program of any size. This sectionwill cover the core collections found <strong>in</strong> the System.Collections.Generic namespace.We’ll start <strong>in</strong> familiar territory with List.3.5.1 ListWe’ve already seen List several times. Broadly speak<strong>in</strong>g, it’s the generic equivalentof the nongeneric ArrayList type, which has been a part of .NET from the wordgo. There are some new features, and a few operations <strong>in</strong> ArrayList didn’t make it toList. Most of the features that have been removed from List have also beenremoved from other collections, so we’ll cover them here and then just refer to themlater on when talk<strong>in</strong>g about the other collections. Many of the new features <strong>in</strong>List (beyond “be<strong>in</strong>g generic”) aren’t available <strong>in</strong> the other generic collections.The comb<strong>in</strong>ation of these factors leads to our discussion of List be<strong>in</strong>g the longest<strong>in</strong> this section—but then it’s probably the most widely used collection <strong>in</strong> real-lifecode, too. When you th<strong>in</strong>k of us<strong>in</strong>g a list of data items <strong>in</strong> your code, List is thedefault choice.I won’t bore you with the most common operations (add<strong>in</strong>g, remov<strong>in</strong>g, fetch<strong>in</strong>g,and replac<strong>in</strong>g items) but will merely po<strong>in</strong>t out that List makes itself available <strong>in</strong> alarge number of situations us<strong>in</strong>g old APIs by implement<strong>in</strong>g IList as well as IList.Enough of look<strong>in</strong>g backward, though—let’s see what’s new.NEW FEATURES OF LISTThe new methods available with<strong>in</strong> List are all powered by generics—<strong>in</strong> particular,generic delegates. This is part of a general trend toward us<strong>in</strong>g delegates more heavily<strong>in</strong> the framework, which has been made simpler by the improvements <strong>in</strong> delegate syntaxavailable <strong>in</strong> <strong>C#</strong> 2. (There would have been little po<strong>in</strong>t <strong>in</strong> add<strong>in</strong>g lots of delegatespecificfeatures <strong>in</strong>to the framework with the syntax be<strong>in</strong>g as clunky as it was <strong>in</strong> <strong>C#</strong> 1.)We can now do the follow<strong>in</strong>g:■ Convert each element of the list to a different type, result<strong>in</strong>g <strong>in</strong> a new list(ConvertAll).■ Check whether any of the elements <strong>in</strong> the list match a given predicate (Exists).■ Check whether all of the elements <strong>in</strong> the list match a given predicate(TrueForAll).■ F<strong>in</strong>d the first, last, or all elements <strong>in</strong> the list match<strong>in</strong>g a predicate (F<strong>in</strong>dXXX).Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!