令人惊喜的泛型委托 Predicate/Func/Action
Predicate 泛型委托
表示定义一组条件并确定指定对象是否符合这些条件的方法。此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素。
看看下面它的定义:
// Summary:
// Represents the method that defines a set of criteria and determines whether
// the specified object meets those criteria.
//
// Parameters:
// obj:
// The object to compare against the criteria defined within the method represented
// by this delegate.
//
// Type parameters:
// T:
// The type of the object to compare.
//
// Returns:
// true if obj meets the criteria defined within the method represented by this
// delegate; otherwise, false.
public delegate bool Predicate<T>(T obj);
// Represents the method that defines a set of criteria and determines whether
// the specified object meets those criteria.
//
// Parameters:
// obj:
// The object to compare against the criteria defined within the method represented
// by this delegate.
//
// Type parameters:
// T:
// The type of the object to compare.
//
// Returns:
// true if obj meets the criteria defined within the method represented by this
// delegate; otherwise, false.
public delegate bool Predicate<T>(T obj);
类型参数介绍:
T: 要比较的对象的类型。
obj: 要按照由此委托表示的方法中定义的条件进行比较的对象。
返回值:如果 obj 符合由此委托表示的方法中定义的条件,则为 true;否则为 false。
看下面代码:
public class GenericDelegateDemo
{
List<String> listString = new List<String>()
{
"One","Two","Three","Four","Fice","Six","Seven","Eight","Nine","Ten"
};
String[] arrayString = new String[]
{
"One","Two","Three"
{
List<String> listString = new List<String>()
{
"One","Two","Three","Four","Fice","Six","Seven","Eight","Nine","Ten"
};
String[] arrayString = new String[]
{
"One","Two","Three"
补充:软件开发 , C# ,