More Effective C# 第21条、第22条 实例代码解析(可比较 可排序 自定义对象集合完整演示) IEquatable, IComparable,IEnumerable...

More Effective C# 第21条为:“让重载方法尽可能清晰,最小化且完整”,第22条为:“定义方法后再重载操作符”。

具体文字我就不多描述了,“翠花,上代码”:

person类:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ExamNegateVirtualEvent { public class Person : IEquatable<Person>, IComparable<Person> { public string Id { get; set; } public int Age { get; set; } public string FirstName { get; set; } public string LastName { get; set; } /// <summary> /// Initializes a new instance of the <see cref="Person"/> class. /// </summary> /// <param name="_age">The _age.</param> /// <param name="fname">The fname.</param> /// <param name="lName">Name of the l.</param> public Person(int _age, string fName, string lName) { Id = _age.ToString(); Age = _age; FirstName = fName; LastName = lName; } #region IEquatable<Person> Members bool IEquatable<Person>.Equals(Person other) { return this.FirstName == other.FirstName && this.LastName == other.LastName; } public static bool operator ==(Person a, Person b) { // If both are null, or both are same instance, return true. if (System.Object.ReferenceEquals(a, b)) { return true; } // If one is null, but not both, return false. if (((object)a == null) || ((object)b == null)) { return false; } // Return true if the fields match: return a.FirstName == b.FirstName && a.LastName == b.LastName; } public static bool operator !=(Person a, Person b) { return !(a == b); } #endregion #region IComparable<Person> Members /// <summary> /// other.Age-this.Age 倒序 /// this.Age - other.Age 顺序 /// </summary> /// <param name="other"></param> /// <returns></returns> int IComparable<Person>.CompareTo(Person other) { return this.Age - other.Age; } public static bool operator >(Person a, Person b) { return (a.Age>b.Age); } public static bool operator >=(Person a, Person b) { return (a.Age >= b.Age); } public static bool operator <(Person a, Person b) { return (a.Age < b.Age); } public static bool operator <=(Person a, Person b) { return (a.Age <= b.Age); } #endregion /// <summary> /// 重载父类的ToString方法 /// </summary> /// <returns></returns> public override string ToString() { return string.Format("Person Info: name:{0} age:{1}", this.FirstName + " " + this.LastName, this.Age.ToString()); } } }

PersonCollection类:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ExamNegateVirtualEvent { public class PersonCollection:IEnumerable { private List<Person> _persons; // Person类的集合 public void AddPerson(Person _p) //向集合插入一个Person,并对所有的人员重新排序 { _persons.Add(_p);//插入 _persons.Sort(); //排序 } public PersonCollection() { _persons = new List<Person>(); } #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() { foreach (Person item in _persons) { yield return item; } } #endregion } }

测试代码:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ExamNegateVirtualEvent { class Program { static void Main(string[] args) { testObjectSort(); } static void testObjectSort() { PersonCollection persons = new PersonCollection(); Person p1 = new Person(25, "Anders", "lu"); Person p2 = new Person(25, "Anders", "wang"); Person p3 = new Person(30, "Anders", "zhao"); Person p4 = new Person(10, "Anders", "sun"); Person p5 = new Person(40, "Anders", "qian"); Console.WriteLine(string.Format("p1==p2 is {0}",(p1==p2).ToString())); Console.WriteLine(string.Format("p1>=p2 is {0}", (p1 >= p2).ToString())); persons.AddPerson(p1); persons.AddPerson(p2); persons.AddPerson(p3); persons.AddPerson(p4); persons.AddPerson(p5); foreach (Person item in persons) { Console.WriteLine(item.ToString()); } Console.ReadLine(); } } }

简单说明一点,yield return语句,C#2.0之后添加的的该语句,用来支持以序列的方式返回对象集合,该语句不但返回一个值,而且还保留了内部迭代中的当前位置及当前状态。以前不咋重视该语句,可看了More Effective C#感觉这个语句真的很优秀。比如可以减少一层循环遍数。

整理了一下,感觉思路清楚了好多呀,以后看书还是要多敲代码测试的,呵呵。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值