Generic Delegates for Manipulating Arrays and Lists

This topic provides an overview of generic delegates for conversions, search predicates, and actions to be taken on elements of an array or collection.

Generic Delegates for Manipulating Arrays and Lists

The Action<T> generic delegate represents a method that performs some action on an element of the specified type. You can create a method that performs the desired action on the element, create an instance of the Action<T> delegate to represent that method, and then pass the array and the delegate to the Array.ForEach static generic method. The method is called for every element of the array.

The List<T> generic class also provides a ForEach method that uses the Action<T> delegate. This method is not generic.

Note

This makes an interesting point about generic types and methods. The Array.ForEach method must be static (Shared in Visual Basic) and generic because Array is not a generic type; the only reason you can specify a type for Array.ForEach to operate on is that the method has its own type parameter list. By contrast, the nongeneric List<T>.ForEach method belongs to the generic class List<T>, so it simply uses the type parameter of its class. The class is strongly typed, so the method can be an instance method.

The Predicate<T> generic delegate represents a method that determines whether a particular element meets criteria you define. You can use it with the following static generic methods of Array to search for an element or a set of elements: Exists, Find, FindAll, FindIndex, FindLast, FindLastIndex, and TrueForAll.

Predicate<T> also works with the corresponding nongeneric instance methods of the List<T> generic class.

The Comparison<T> generic delegate allows you to provide a sort order for array or list elements that do not have a native sort order, or to override the native sort order. Create a method that performs the comparison, create an instance of the Comparison<T> delegate to represent your method, and then pass the array and the delegate to the Array.Sort<T>(T[], Comparison<T>) static generic method. The List<T> generic class provides a corresponding instance method overload, List<T>.Sort(Comparison<T>).

The Converter<TInput,TOutput> generic delegate allows you to define a conversion between two types, and to convert an array of one type into an array of the other, or to convert a list of one type to a list of the other. Create a method that converts the elements of the existing list to a new type, create a delegate instance to represent the method, and use the Array.ConvertAll generic static method to produce an array of the new type from the original array, or the List<T>.ConvertAll<TOutput>(Converter<T,TOutput>) generic instance method to produce a list of the new type from the original list.

Chaining Delegates

Many of the methods that use these delegates return an array or list, which can be passed to another method. For example, if you want to select certain elements of an array, convert those elements to a new type, and save them in a new array, you can pass the array returned by the FindAll generic method to the ConvertAll generic method. If the new element type lacks a natural sort order, you can pass the array returned by the ConvertAll generic method to the Sort<T>(T[], Comparison<T>) generic method.

See also