Enough of looking backward, though??”let??™s see what??™s new.
NEW FEATURES OF LIST
The new methods available within List are all powered by generics??”in particular,
generic delegates. This is part of a general trend toward using delegates more heavily
in the framework, which has been made simpler by the improvements in delegate syntax
available in C# 2. (There would have been little point in adding lots of delegatespecific
features into the framework with the syntax being as clunky as it was in C# 1.)
We can now do the following:
?– Convert each element of the list to a different type, resulting in a new list
(ConvertAll).
?– Check whether any of the elements in the list match a given predicate (Exists).
?– Check whether all of the elements in the list match a given predicate
(TrueForAll).
?– Find the first, last, or all elements in the list matching a predicate (FindXXX).
97 Generic collection classes in .NET 2.0
?– Remove all elements in the list matching a given predicate (RemoveAll).
?– Perform a given action on each element on the list (ForEach).10
We??™ve already seen the ConvertAll method in listing 3.2, but there are two more delegate
types that are very important for this extra functionality: Predicate and
Action, which have the following signatures:
public delegate bool Predicate (T obj)
public delegate void Action (T obj)
A predicate is a way of testing whether a value matches a criterion.
Pages:
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210