overview
Below are generic delegates useful for manipulating arrays and lists.
Action<T>
Represents a method that performs some action on an element of the specified type.
Usage:
- Create a method that performs the desired action on an element
- Create an instance of the
Action<T>delegate - Pass the array and the delegate to the
Array.ForEachstatic generic method
Note: List<T> also has a ForEach method that uses an Action<T> delegate, but this method is not generic.
Predicate<T>
Represents a method that determines whether a particular element meets defined criteria.
Usage:
- Create a method that performs the desired test
- Create an instance of the
Predicate<T>delegate - Call
Array’s methods that use this delegate:Exists,Find,FindAll,FindIndex,FindLast,FindLastIndex,TrueForAll.
Note: List<T> also has instance methods that accept Predicate<T>.
Comparison<T>
Use to provide a sort order for an array or list that does not have a native sort order.
Usage:
- Create a method that performs the comparison
- Create an instance of the
Comparison<T>delegate - Call the
Array.Sort<T>orList<T>.Sort()methods
Converter<T>
Use to define a conversion between two types including between arrays of different types and between lists of different types.
Usage:
- Create a method that converts the elements of the existing list to a new type
- Create an instance of the
Converter<T>delegate - Call the
Array.ConvertAllorList<T>.ConvertAllmethods