日期:2014-05-20  浏览次数:20743 次

关于泛型,请高手过来看看。
今天学习泛型,看得晕呼晕呼的,请大侠帮忙解析一下泛型主要做什么用,在业务逻辑上可以解决什么问题。还有下面的代码怎么去理解。
#region   Using   directives

using   System;
using   System.Collections.Generic;
using   System.Text;

#endregion

namespace   ListDemo
{
public   class   FindRacer
{
private   string   car;
public   FindRacer(string   car)
{
this.car   =   car;
}

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
}

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   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;
}

}


class   Program
{
static   void   Main(string[]   args)
{
List <Racer>   racers   =   new   List <Racer> ();
racers.Add(new   Racer( "Michael   Schumacher ",   "Ferrari "));
racers.Add(new   Racer( "Juan   Pablo   Montoya ",   "McLaren-Mercedes "));
racers.Add(new   Racer( "Kimi   Raikkonen ",   "McLaren-Mercedes "));