Windows Phone 7手机开发.NET培训期待与您交流!
Racer类有三个字段:firstname、lastname和获胜者的数量。这些字段可以用属性来访问。在该类的构造函数中,可以传送赛手的姓名和获胜者的数量,以设置成员。方法ToString()重写为返回赛手的姓名。类Racer也实现了泛型接口IComparer<T>,为Racer元素排序。
[Serializable]
public class Racer : IComparable<Racer>, IFormattable
{
public Racer()
: this(String.Empty, String.Empty, String.Empty) {}
public Racer(string firstname, string lastname, string country)
: this(firstname, lastname, country, 0) {}
public Racer(string firstname, string lastname, string country, int wins)
{
this.firstname = firstname;
this.lastname = lastname;
this.country = country;
this.wins = wins;
}
private string firstname;
public string Firstname
{
get {return firstname; }
set { firstname = value; }
}
private string lastname;
public string Lastname
{
get {return lastname; }
set { lastname = value; }
}
private string country;
public string Country
{
get {return country; }
set { country = value; }
}
private int wins;
public int Wins
{
get {return wins; }
set { wins = value; }
}
public override string ToString()
{
return firstname + " " + lastname;
}
public string ToString(string format, IFormatProvider formatProvider)
{
switch (format)
{
case null:
case "N": //Name
return ToString();
case "F": //FirstName
return Firstname;
case "L": //LastName
return Lastname;
case "W": //Wins
return ToString() + " Wins: " + wins;
case "C": //Country
return ToString() + " Country: " + country;
case "A": //All
return ToString() + ", " + country + " Wins: " + wins;
default:
throw new FormatException(String.Format(formatProvider,
"Format {0} is not supported", format));
}
}
public string ToString(string format)
{
return ToString(format, null);
}
public int CompareTo(Racer other)
{
return this.lastname. CompareTo(other.lastname);
}
}
1. 添加元素
使用Add()方法可以给列表添加元素,如下所示。实例化的泛型类型用Add()方法定义了第一个参数的类型:
List<int> intList = new List<int>();
intList.Add(1);
intList.Add(2);
List<string> stringList = new List<string>();
stringList.Add("one");
stringList.Add("two");
变量racers定义为类型List<Racer>。使用new操作符创建相同类型的一个新对象。因为类List<T>用具体类Racer来实例化,所以现在只有Racer对象可以用Add()方法添加。在下面的例子中,创建了4个一级方程式赛手,并添加到集合中。
List<Racer> racers = new List<Racer>(20);
Racer graham = new Racer("Graham", "Hill", "UK", 14);
Racers.Add(graham);
Racer emerson = new Racer("Emerson", "Fittipaldi", "Brazil", 14);
Racers.Add(emerson);
Racer mario = new Racer("Mario", "Andretti", "USA", 12);
Racers.Add(Mario);
racers.Add(new Racer("Michael", "Schumacher", "Germany", 91));
racers.Add(new Racer("Mika", "Hakkinen", "Finland", 20));
使用List<T>类的AddRange()方法,可以一次给集合添加多个元素。AddRange()方法的参数是IEnumerable<T>类型的对象,所以也可以传送一个数组,如下所示:
racers.AddRange(new Racer[] {
new Racer("Niki", "Lauda", "Austria", 25)
new Racer("Alian", "Prost", "France", 51 } );
如果在实例化列表时知道集合的元素个数,也可以将执行IEnumerable<T>的任意对象传送给类的构造函数。这非常类似于AddRange()方法:
List<Racer> racers = new List<Racer> (new Racer[] {
new Racer("Jochen", "Rindt", "Austria", 6)
new Racer("Ayrton", "Senna", "Brazil", 41 } );
2. 插入元素
使用Insert()方法可以在指定位置插入元素:
racers. Insert(3, new Racer("Phil", "Hill", "USA", 3));
方法InsertRange()提供了插入大量元素的容量,类似于前面的AddRange()方法。
如果索引集大于集合中的元素个数,就抛出ArgumentOutOfRangeException类型的异常。
3. 访问元素
执行了IList和IList<T>接口的所有类都提供了一个索引器,所以可以使用索引器,通过传送元素号来访问元素。第一个元素可以用索引值0来访问。
Racer r1 = racers[3];
可以用Count属性确定元素个数,再使用for循环迭代集合中的每个元素,使用索引器访问每一项:
for (int i=0; i<racers.Count; i++)
{
Console.WriteLine(racers[i]);
}
4. 删除元素
删除元素时,可以利用索引,或传送要删除的元素。下面的代码把3传送给RemoveAt(),删除第4个元素:
racers. RemoveAt(3);
也可以直接将Racer对象传送给Remove()方法,删除这个元素。按索引删除比较快,因为必须在集合中搜索要删除的元素。Remove()方法先在集合中搜索,用IndexOf()方法确定元素的索引,再使用该索引删除元素。IndexOf()方法先检查元素类型是否执行了IEquatable接口。如果是,就调用这个接口中的Equals()方法,在集合中查找传送给Equals()方法的元素。如果没有执行这个接口,就使用Object类的Equals()方法比较元素。Object类的Equals()方法的默认实现代码对值类型进行按位比较,对引用类型只比较其引用。