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 System.Action 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 System.Action delegate to represent that method, and then pass the array and the delegate to the System.Array.ForEach static generic method. The method is called for every element of the array.
The System.Collections.Generic.List generic class also provides a System.Collections.Generic.List.ForEach method that uses the System.Action delegate. This method is not generic.
The System.Predicate 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 System.Array to search for an element or a set of elements: System.Array.Exists, System.Array.Find, System.Array.FindAll, System.Array.FindIndex, System.Array.FindLast, System.Array.FindLastIndex, and System.Array.TrueForAll.
System.Predicate also works with the corresponding nongeneric instance methods of the System.Collections.Generic.List generic class.
The System.Comparison 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 System.Comparison delegate to represent your method, and then pass the array and the delegate to the [System.Array.Sort%28%5B%5D%2CSystem.Comparison%7B`%7D%29](https://learn.microsoft.com/en-us/dotnet/api/system.array.sort--1%28--0%5b%5d%2csystem.comparison%7b--0%7d%29) static generic method. The System.Collections.Generic.List generic class provides a corresponding instance method overload, System.Collections.Generic.List.Sort%28System.Comparison%7B%7D%29.
The System.Converter 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 System.Array.ConvertAll generic static method to produce an array of the new type from the original array, or the System.Collections.Generic.List.ConvertAll%28System.Converter%7B%2C%7D%29 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 System.Array.FindAll generic method to the System.Array.ConvertAll generic method. If the new element type lacks a natural sort order, you can pass the array returned by the System.Array.ConvertAll generic method to the [System.Array.Sort%28%5B%5D%2CSystem.Comparison%7B`%7D%29](https://learn.microsoft.com/en-us/dotnet/api/system.array.sort--1%28--0%5b%5d%2csystem.comparison%7b--0%7d%29) generic method.