用于操作数组和列表的泛型委托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

Action<T> 泛型委托表示对指定类型的元素执行某些操作的方法。The Action<T> generic delegate represents a method that performs some action on an element of the specified type. 你可以创建一种对元素执行所需操作的方法,创建 Action<T> 委托的实例来表示该方法,然后将该数组和委托传递给 Array.ForEach 静态泛型方法。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.

List<T> 泛型类还提供了 ForEach 方法,该方法使用 Action<T> 委托。The List<T> generic class also provides a ForEach method that uses the Action<T> delegate. 此方法不属泛型方法。This method is not generic.

备注

这使泛型类型和方法变得有趣。This makes an interesting point about generic types and methods. Array.ForEach 方法必须是静态(在 Visual Basic 中为 Shared)和泛型的,因为 Array 不是泛型类型;你可以对 Array.ForEach 指定一种类型以继续运行的唯一原因是该方法有自己的类型参数列表。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. 与之相反,非泛型 List<T>.ForEach 方法属于泛型类 List<T>,因此它仅使用其类的类型参数。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.

Predicate<T> 泛型委托表示的是用于确定特定元素是否满足你定义的标准的方法。The Predicate<T> generic delegate represents a method that determines whether a particular element meets criteria you define. 你可以将其用于 Array 的以下静态泛型方法来搜索一个或一组元素:ExistsFindFindAllFindIndexFindLastFindLastIndexTrueForAllYou 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> 也适用于 List<T> 泛型类相应的非泛型实例方法。Predicate<T> also works with the corresponding nongeneric instance methods of the List<T> generic class.

Comparison<T> 泛型委托让你能为不具有本机排序顺序的数组或列表元素提供顺序排序或重写本机排序顺序。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. 创建执行比较的方法,创建一个 Comparison<T> 委托的实例,以表示你的方法,然后将该数组和委托传递给 Array.Sort<T>(T[], Comparison<T>) 静态泛型方法。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. List<T> 泛型类提供相应的实例方法重载,List<T>.Sort(Comparison<T>)The List<T> generic class provides a corresponding instance method overload, List<T>.Sort(Comparison<T>).

Converter<TInput,TOutput>泛型委托让你能定义两个类型之间的转换,将一个类型的数组转换到另一个类型的数组,或者将一个类型的列表转换到另一个类型的列表。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. 创建将现有列表元素转换为新类型的方法,创建一个委托实例来表示该方法,并使用 Array.ConvertAll 泛型静态方法,从原始数组生成新类型数组;或使用 List<T>.ConvertAll<TOutput>(Converter<T,TOutput>) 泛型实例方法,从原始列表生成新类型列表。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. 例如,如果你想要选择某些数组元素,将这些元素转换为新类型,并将其保存在新的数组中,则可以将 FindAll 泛型方法返回的数组传递到 ConvertAll 泛型方法。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. 如果新的元素类型缺少自然排序顺序,你可以将 ConvertAll 泛型方法返回的数组传递到 Sort<T>(T[], Comparison<T>) 泛型方法。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