示例
using System;
using System.Collections.Generic;
namespace PredicateTest
{
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(2);
list.TrimExcess();//将容量设置为实际元素数目
list.Find(delegate(int result)
{
if(result == 2)
{
Console.WriteLine("Find: 2");
return true;
}
return false;
});
list.FindAll(delegate(int result)
{
if (result == 2)
{
Console.WriteLine("FindAll: 2");
return true;
}
return false;
});
Console.ReadLine();
}
}
}
运行测试
namespace System
{
// 摘要:
// 表示定义一组条件并确定指定对象是否符合这些条件的方法。
//
// 参数:
// obj:
// 要按照由此委托表示的方法中定义的条件进行比较的对象。
//
// 类型参数:
// T:
// 要比较的对象的类型。
//
// 返回结果:
// 如果 obj 符合由此委托表示的方法中定义的条件,则为 true;否则为 false。
public delegate bool Predicate<in T>(T obj);
}