码迷,mamicode.com
首页 > Windows程序 > 详细

读改善c#代码157个建议:建议10~11

时间:2015-07-17 08:22:36      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

目录:

  • 建议10:创建对象时需要考虑是否实现比较器
  • 建议11:区别对待==与Equals

 

一、建议10:创建对象时需要考虑是否实现比较器

比较一下基本工资:

 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();

 输出:

技术分享

 

二、建议11:区别对待==与Equals

两者都是指相等性,即:值相等性和引用相等性。

值类型:如果值类型相等,返回True。

引用类型:如果指向同一个引用,返回True。

很好理解,举个例子:

1、值类型:==与Equls()

            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();

运行:

技术分享

 

2、引用类型

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

 

3、引用类型重载Equals()达到值类型比较效果

还有一点,有时我们需要我们的类型看上去和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方法比较实例是否相同。验证引用的相等性。

 

读改善c#代码157个建议:建议10~11

标签:

原文地址:http://www.cnblogs.com/sunchong/p/4652687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!