
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
318
|
Chapter 5: Collections
The TrueForAll method accepts a generic delegate Predicate<T> called match and
returns a Boolean value:
public bool TrueForAll(Predicate<T> match)
The match parameter determines whether or not a true or false should be returned
by the
TrueForAll method.
The
TrueForAll method basically consists of a loop that iterates over each element in
the collection. Within this loop, a call to the
match delegate is invoked. If this dele-
gate returns
true, the processing continues on to the next element in the collection. If
this delegate returns
false, processing stops and a false is returned by the
TrueForAll method. When the TrueForAll method is finished iterating over all the
elements of the collection and the match delegate has not returned a
false value for
any element, the
TrueForAll method returns a true.
See Also
See the “Array Class,” “List<T> Class,” and “TrueForAll Method” topics in the
MSDN documentation.
5.16 Performing an Action on Each Element
in an Array or List<T>
Problem
You need an easy way to iterate over all the elements in an Array or List<T>, per-
forming an operation on each element as you go.
Solution
Use the ForEach method of the Array or List<T> classes:
// Create and populate a List of Data objects.
List<Data> numbers = new List<Data>( );
numbers.Add(new Data(1)); ...