关于C#中==与Equals方法的区别总结

来源:互联网 发布:网络协议与分析 编辑:程序博客网 时间:2024/06/16 00:26
说明: 这是个人的一家之见,自己查阅资料总结而出,基本指明了C#中==与Equals方法的区别
为了保护原创作者的原创性,请您先参考一下文章,之后回来做一个讨论交流:
http://www.cnblogs.com/jiahaipeng/archive/2008/04/11/1146316.html#commentform

为了方便理解,我把源代码粘贴过来:
//class Person

?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespaceConsoleApplication1
{
    classPerson
    {
        private stringname;
        public stringName
        {
            get { returnname; }
            set { name = value; }
        }
        public Person(stringname)
        {
            this.name = name;
        }
    }
}

//Main

?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespaceConsoleApplication1
{
    classProgram
    {
        static voidMain(string[] args)
        {
            string a = new string(newchar[] { 'h','e', 'l', 'l','o' });
            string b = new string(newchar[] { 'h','e', 'l', 'l','o' });
            Console.WriteLine(a == b);
            Console.WriteLine(a.Equals(b));
            object g = a;
            object h = b;
            Console.WriteLine(g == h);
            Console.WriteLine(g.Equals(h));
            Person p1 = newPerson("jia");
            Person p2 = newPerson("jia");
            Console.WriteLine(p1 == p2);
            Console.WriteLine(p1.Equals(p2));
            Person p3 = newPerson("jia");
            Person p4 = p3;
            Console.WriteLine(p3 == p4);
            Console.WriteLine(p3.Equals(p4));
            Console.ReadLine();
        }
    }
}

输出结果:true,true,false,true,false,false,true,true。其详细解释请参考一下网页:
http://www.cnblogs.com/jiahaipeng/archive/2008/04/11/1146316.html#commentform


总结如下:

1、对于值类型,==和equals等价,都是比较存储信息的内容。
2、对于引用类型,==比较的是引用类型在栈中的地址,equals方法则比较的是引用类型在托管堆中的存储信息的内容。
3、对于string类要特殊处理,它是一个内部已经处理好了equals方法和==的类,故==和equals等价,都是比较存储信

    息的内容。
4、对于一些自定义的类,我们有必要重载equals方法,否则它默认为基类的equals方法(基类没有重载Equals方法则为
    Object类中的Equals方法),他们的比较也为地址,而不是引用类型在托管堆中的存储信息的内容。故我们就不难理解
     以下输出了:
               Person p1 = new Person("jia");
    Person p2 = new Person("jia");
    Console.WriteLine(p1 == p2);//输出False
    Console.WriteLine(p1.Equals(p2));//输出False

5、对于string我们有必要强调一下常量字符串与字符串变量的区别,请看一下例子:
           //
常量字符串
            string x = "should it matter"; //指向同一个地址,即所谓的常量池
            string y = "should it matter";
            object c = x;
            object d = y;
            Console.WriteLine(c == d);//
输出True
            Console.WriteLine(c.Equals(d));//
输出True
           //
字符串变量
            string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });//指向的地址不一样,是动态分配的
            string b = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
            object g = a;
            object h = b;
            Console.WriteLine(g == h);//
输出False
            Console.WriteLine(g.Equals(h));//
输出True
  他们分配地址的方式不一样,string x = "should it  matter";由于它的初始值是一个常量,所以其地址分配在托
管堆上的静态存储区,即所谓的常量池,而楼主的string a = new  string(new char[] { 'h', 'e', 'l', 'l', 'o'});则不
一样,它这是在托管堆上动态分配的地址。

      详细的解释可以参考一下网址或者查阅msdn:

      http://www.cnblogs.com/instance/archive/2011/05/24/2056091.html

原创粉丝点击