比较一下基本工资:
class Salary : IComparable { public string Name { get; set; } public decimal BaseSalary { get; set; }
public decimal Bonus { get; set; } public int CompareTo(object obj) { Salary comparer = obj as Salary; if (BaseSalary > comparer.BaseSalary) { return 1; } else if (BaseSalary == comparer.BaseSalary) { return 0; } else { return -1; } } }
客户端调用:
List<Salary> salaries = new List<Salary>(); salaries.Add(new Salary() { Name = "Sun", BaseSalary = 1000 }); salaries.Add(new Salary() { Name = "Yuan", BaseSalary = 2000 }); salaries.Add(new Salary() { Name = "Kun", BaseSalary = 3000 }); salaries.Add(new Salary() { Name = "Qun", BaseSalary = 3000 }); salaries.Add(new Salary() { Name = "Sun", BaseSalary = 4000 }); salaries.Sort(); foreach (var s in salaries) { Console.WriteLine("【Name】:{0},【BaseSalary】:¥{1}{2}", s.Name, s.BaseSalary,System.Environment.NewLine); } Console.ReadKey();
运行:
如果不想用基本工资BaseSalary进行排序,而是以奖金Bonus进行排序,使用IComparer实现自定义比较器:
class BonusComparer : IComparer<Salary> { public int Compare(Salary x, Sarlary y) { return left.Bonus.CompareTo(right.Bonus); } }
客户端提供我们上面创建的比较器:
List<Salary> salaries = new List<Salary>(); salaries.Add(new Salary() { Name = "Sun", BaseSalary = 1000,Bonus=4000 }); salaries.Add(new Salary() { Name = "Yuan", BaseSalary = 2000, Bonus = 3000 }); salaries.Add(new Salary() { Name = "Kun", BaseSalary = 3000, Bonus = 2000 }); salaries.Add(new Salary() { Name = "Qun", BaseSalary = 3000,Bonus=4000 }); salaries.Add(new Salary() { Name = "Dun", BaseSalary = 4000,Bonus=0 }); salaries.Sort(new BonusComparer()); foreach (var s in salaries) { Console.WriteLine("Name:【{0}】,BaseSalary:¥{1},Bonus:{2}{3}", s.Name, s.BaseSalary,s.Bonus, System.Environment.NewLine); } Console.ReadKey();
输出:
两者都是指相等性,即:值相等性和引用相等性。
很好理解,举个例子:
int x = 1; int y = 1; Console.WriteLine("int x=1;{0}int y=1;{0}", System.Environment.NewLine,System.Environment.NewLine); Console.WriteLine("x==y:{0}",x == y); Console.WriteLine("x.Equals(y):{0}{1}",x.Equals(y),System.Environment.NewLine); x = y; Console.WriteLine("x=y;{0}", System.Environment.NewLine); Console.WriteLine("x==y:{0}", x == y); Console.WriteLine("x.Equals(y):{0}", x.Equals(y)); Console.ReadKey();
运行:
class People { public String Name { get; set; } }
客户端:
People p1 = new People() { Name = "Sun" }; People p2 = new People() { Name = "Yuan" }; Console.WriteLine("People p1 = new People();{0}People p2 = new People();{1}", System.Environment.NewLine, System.Environment.NewLine); Console.WriteLine("p1==p2:{0}", p1 == p2); Console.WriteLine("p1.Equals(p2):{0}{1}", p1.Equals(p2), System.Environment.NewLine); Console.WriteLine("------------------------------------"); p1 = p2; p1.Name = "Moon"; Console.WriteLine("p1=p2;{0}", System.Environment.NewLine); Console.WriteLine("p1==p2:{0}", p1 == p2); Console.WriteLine("p1.Equals(p2):{0}", p1.Equals(p2));
运行:
后面我们修改了p1里Name="Moon"的值,但是,p2的Name值也变成了Moon。 所 以,==与Equal()在比较引用类型时,引用地址一样,返回True
还有一点,有时我们需要我们的类型看上去和string类型类似,有值类型的感觉。所以说,我们的这个引用类型,需要重载==或者Equals()。
这里建议只重载Equals()来达到像值类型一样的比较效果。保留==,保留引用比较。例如:生活中我们认为身份证号码一样的是同一个人。
class People { public String Name { get; set; } public string IDCode { get; set; } public override bool Equals(object obj) { People p = obj as People; return p.IDCode == IDCode; } }
客户端:
People p1 = new People() { IDCode="No1" }; People p2 = new People() { IDCode = "No1" }; Console.WriteLine("People p1 = new People();{0}People p2 = new People();{1}", System.Environment.NewLine, System.Environment.NewLine); Console.WriteLine("p1.IDCode={0}", p1.IDCode); Console.WriteLine("p2.IDCode={0}", p2.IDCode); Console.WriteLine(); Console.WriteLine("p1==p2:{0}【保留引用地址的对比】", p1 == p2); Console.WriteLine("p1.Equals(p2):{0}【重载比较IDCode,值类型比较效果】{1}", p1.Equals(p2), System.Environment.NewLine); Console.WriteLine("----------------------------------"); p1 = p2; p1.IDCode = "No2"; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("p1.IDCode={0}", p1.IDCode); Console.WriteLine("p2.IDCode={0}", p2.IDCode); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("p1=p2;{0}", System.Environment.NewLine); Console.WriteLine("p1==p2:{0}", p1 == p2); Console.WriteLine("p1.Equals(p2):{0}", p1.Equals(p2)); Console.ReadKey();
运行:
还有,Object.ReferenceEquals方法比较实例是否相同。验证引用的相等性。