- //源代码下载路径:http://media.wiley.com/product_ancillary/41/07645753/DOWNLOAD/Ch10.zip
-
namespace ListDemo
-
{
-
using System;
-
using System.Collections.Generic;
-
-
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 name;
-
}
-
}
-
-
public class Person
-
{
-
private string firstname;
-
public string Firstname
-
{
-
get { return firstname; }
-
set { firstname = value; }
-
}
-
-
private string lastname;
-
public string Lastname
-
{
-
get { return lastname; }
-
set { lastname = value; }
-
}
-
-
public Person(string firstname, string lastname)
-
{
-
this.firstname = firstname;
-
this.lastname = lastname;
-
}
-
-
public override string ToString()
-
{
-
return firstname + " " + lastname;
-
}
-
}
-
-
public class FindRacer
-
{
-
private string car;
-
public FindRacer(string car)
-
{
-
this.car = car;
-
}
-
-
/// <summary>
-
/// 谓词方法--Predicate<T>泛型委托调用的方法
-
/// public delegate bool Predicate<T>(T obj)--委托定义
-
/// </summary>
-
public bool DrivingCarPredicate(Racer racer)
-
{
-
return racer.Car == car;
-
}
-
}
-
-
public class RacerComparer : IComparer<Racer>
-
{
-
public enum CompareType
-
{
-
Name,
-
Car
-
}
-
-
private CompareType compareType;
-
-
public RacerComparer(CompareType compareType)
-
{
-
this.compareType = compareType;
-
}
-
-
#region IComparer<Racer> Members
-
public int Compare(Racer x, Racer y)
-
{
-
int result = 0;
-
switch (compareType)
-
{
-
case CompareType.Name:
-
result = x.Name.CompareTo(y.Name);
-
break;
-
case CompareType.Car:
-
result = x.Car.CompareTo(y.Car);
-
break;
-
}
-
return result;
-
}
-
#endregion
-
}
-
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
//初始化
-
List<Racer> list = new List<Racer>();
-
//添加对象--这里只能使用Racer类的实例,体现的就是泛型的类型安全
-
list.Add(new Racer("Michael Schumacher", "Ferrari"));
-
list.Add(new Racer("Juan Pablo Montoya", "McLaren-Mercedes"));
-
list.Add(new Racer("Kimi Raikkonen", "McLaren-Mercedes"));
-
list.Add(new Racer("Mark Webber", "Williams-BMW"));
-
list.Add(new Racer("Rubens Barichello", "Ferrari"));
-
-
#region 1、查找元素--FindAll(Predicate<T> match)|执行操作ForEach(Action<T> action)
-
FindRacer finder = new FindRacer("Ferrari");
-
//foreach (Racer racer in list.FindAll(new Predicate<Racer>(finder.DrivingCarPredicate)))
-
//{
-
// Console.WriteLine(racer);
-
//}//和下面的语句执行相同
-
list.FindAll(new Predicate<Racer>(finder.DrivingCarPredicate))
-
.ForEach(delegate(Racer r) { Console.WriteLine(r); });//ForEach方法中使用匿名委托
-
-
//反编译后的FindAll方法
-
//public List<T> FindAll(Predicate<T> match)
-
//{
-
// if (match == null)
-
// {
-
// ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
-
// }
-
// List<T> list = new List<T>();
-
// for (int i = 0; i < this._size; i++)
-
// {
-
// if (match(this._items[i]))
-
// {
-
// list.Add(this._items[i]);
-
// }
-
// }
-
// return list;
-
//}
-
//反编译后的ForEach方法
-
//public void ForEach(Action<T> action)
-
//{
-
// if (action == null)
-
// {
-
// ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
-
// }
-
// for (int i = 0; i < this._size; i++)
-
// {
-
// action(this._items[i]);
-
// }
-
//}
-
#endregion
-
-
#region 2、对元素排序Sort(IComparer<T>)|Sort(Comparison<T>)
-
//使用指定的Comparison<T>【委托】对整个List<T> 中的元素进行排序--这里是匿名委托--返回整数
-
list.Sort(delegate(Racer r1, Racer r2) { return r1.Name.CompareTo(r2.Name); });
-
//使用指定的比较器对整个 List<T> 中的元素进行排序。
-
list.Sort(new RacerComparer(RacerComparer.CompareType.Car));
-
-
Console.WriteLine(""nSorted:");
-
list.ForEach(delegate(Racer r) { Console.WriteLine(r); });
-
#endregion
-
-
#region 3、类型转换
-
List<Person> persons = list.ConvertAll<Person>(delegate(Racer r)
-
{
-
int ixSeperator = r.Name.LastIndexOf(' ') + 1;
-
string lastname = r.Name.Substring(ixSeperator, r.Name.Length - ixSeperator);
-
string firstname = r.Name.Substring(0, ixSeperator - 1);
-
return new Person(firstname, lastname);
-
});
-
-
Console.WriteLine(""nPersons:");
-
persons.ForEach(delegate(Person p) { Console.WriteLine(p); });
-
#endregion
-
-
Console.ReadLine();
-
}
-
}
-
}