# using System; # using System.Collections.Generic; # using System.Text; # # namespace hgoApp # { # public class Racer # { # private string _Name; # public string Name # { # get { return _Name; } # } # private string _Car; # public string Car # { # get { return _Car; } # } # public Racer( string Name, string Car) # { # this ._Name = Name; # this ._Car = Car; # } # public override string ToString() # { # return string .Format( "{0}开的车是名车{1}" , _Name, _Car); # } # } # class Test # { # static void Main() # { # List<Racer> MyRacer = new List<Racer>(); # //添加对象 # MyRacer.Add(new Racer( "aa" , "宝马" )); # MyRacer.Add(new Racer( "bb" , "奔驰" )); # MyRacer.Add(new Racer( "cc" , "保时捷" )); # MyRacer.Add(new Racer( "dd" , "宝马" )); # foreach (Racer R in MyRacer) # { # Console.WriteLine(R); # } # //查找车型为宝马的车 # FindCar MyCar = new FindCar( "宝马" ); # //new Predicate实际上一个返回类型为bool的委托,这里委托代理的方法是DriveCar # //FindAll找出返回为True的List<Racer>列表 # foreach (Racer R in MyRacer.FindAll( new Predicate<Racer>(MyCar.DriveCar))) # { # Console.WriteLine(R); # } # # } # } # //查找相应的车型 # public class FindCar # { # private string _car; # public FindCar( string car) # { # this ._car = car; # } # //如果在Racer找到对应的Car就返回true # public bool DriveCar(Racer R) # { # return (R.Car == _car) ? true : false ; # } # } # }