IComparable<>,IFormattable,IEnumerable<>接口使用
2014-08-06 19:22
501 查看
1 Racer类
[Serializable]
public class Racer : IComparable<Racer>, IFormattable
{
public Racer(string firstName = null, string lastName = null, string country = null,
int starts = 0, int wins = 0, IEnumerable<int> years = null, IEnumerable<string> cars = null)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Country = country;
this.Starts = starts;
this.Wins = wins;
var yearList = new List<int>();
foreach (int year in years)
{
yearList.Add(year);
}
this.Years = yearList.ToArray();
var carList = new List<string>();
foreach (string car in cars)
{
carList.Add(car);
}
this.Cars = carList.ToArray();
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string Country { get; set; }
public int Starts { get; set; }
public int Wins { get; set; }
public int[] Years { get; private set; }
public string[] Cars { get; private set; }
public int CompareTo(Racer r)
{
if (r == null) throw new ArgumentNullException("r");
return this.LastName.CompareTo(r.LastName);
}
public override string ToString()
{
return string.Format("{0} {1}", FirstName, LastName);
}
public string ToString(string format)
{
return ToString(format, null);
}
public string ToString(string format, IFormatProvider formatProvider)
{
switch (format)
{
case "L":
return LastName;
case "F":
return FirstName;
case "C":
return Country;
case "W":
return Wins.ToString();
case "S":
return Starts.ToString();
default:
throw new FormatException(string.Format("format:{0} not supported!", format));
}
}
}
2 Team类
3 初始化
[Serializable]
public class Racer : IComparable<Racer>, IFormattable
{
public Racer(string firstName = null, string lastName = null, string country = null,
int starts = 0, int wins = 0, IEnumerable<int> years = null, IEnumerable<string> cars = null)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Country = country;
this.Starts = starts;
this.Wins = wins;
var yearList = new List<int>();
foreach (int year in years)
{
yearList.Add(year);
}
this.Years = yearList.ToArray();
var carList = new List<string>();
foreach (string car in cars)
{
carList.Add(car);
}
this.Cars = carList.ToArray();
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string Country { get; set; }
public int Starts { get; set; }
public int Wins { get; set; }
public int[] Years { get; private set; }
public string[] Cars { get; private set; }
public int CompareTo(Racer r)
{
if (r == null) throw new ArgumentNullException("r");
return this.LastName.CompareTo(r.LastName);
}
public override string ToString()
{
return string.Format("{0} {1}", FirstName, LastName);
}
public string ToString(string format)
{
return ToString(format, null);
}
public string ToString(string format, IFormatProvider formatProvider)
{
switch (format)
{
case "L":
return LastName;
case "F":
return FirstName;
case "C":
return Country;
case "W":
return Wins.ToString();
case "S":
return Starts.ToString();
default:
throw new FormatException(string.Format("format:{0} not supported!", format));
}
}
}
2 Team类
public class Team { public Team(string name, params int[] years) { this.Name = name; this.Years = years; } private string Name { get; set; } private int[] Years { get; set; } }
3 初始化
Racer racer = new Racer("Nino", "Farina", "Italy", 33, 5, new int[] { 1950 }, new string[] { "Alfa Romeo", "Maserati" }); Team team1 = new Team("Vanwall", 1955); Team team2 = new Team("Cooper", 1959, 1960);
相关文章推荐
- C#集合与接口(二)IEnumerable<T>接口
- System.Collections.Generic.IEnumerable<T>接口
- 父类实现IComparable<T>接口,子类无法使用~
- 使用HTML判断浏览器版本<![if !IE]>
- 使用泛型 类型“System.Collections.Generic.IEnumerator<T>”需要 1 个类型参数
- 对象的比较与排序(三):实现IComparable<T>和IComparer<T>泛型接口
- “System.Collections.Generic.IEnumerable<decimal>”不包含“ToArray”的定义,并且找不到可接受类型为“System.Collections.Generic.IEnumerable<decimal>”的第一个参数的扩展方法“ToArray”
- 对象的比较与排序(三):实现IComparable<T>和IComparer<T>泛型接口(转)
- IComparable<T>, IComparer<T>,IEnumerable<T>泛型接口
- 《Effective C#》读书笔记——条目22:通过定义并实现接口替代继承<使用C#表达设计>
- 用泛型的IEqualityComparer<T>接口去重复项
- C#中如何使用IComparable<T>与IComparer<T>接口(转载)
- 用泛型的IEqualityComparer<T>接口去重复项
- IEqualityComparer<T>接口
- Linq系列(2)——类型推断,IEnumerable<T>和IQueryable<T>
- 用泛型的IEqualityComparer<T>接口去重复项
- 一图胜千言 – System.Linq.IEnumerable<T> vs System.Linq.IQuerable<T>
- IComparable<T>:泛型接口
- Linq 杂谈(之)IEnumerable<T> 转换 DataTable
- html页<!--[if IE]>...<![endif]-->使用解说