zoukankan      html  css  js  c++  java
  • c# equals与==的区别

    原文地址:http://www.oschina.net/code/snippet_188227_9009

    对于值类型,如果对象的值相等,则相等运算符 (==) 返回 true,否则返回 false。对于string 以外的引用类型,如果两个对象引用同一个对象,则 == 返回 true。对于 string 类型,== 比较字符串的值。 
        ==操作比较的是两个变量的值是否相等。 
        equals()方法比较的是两个对象的内容是否一致.equals也就是比较引用类型是否是对同一个对象的引用。 
        对于值类型的比较,这里就不做描述了,下面讨论引用类型的比较: 
    首先我们看一段程序 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace ConsoleApplication1
    {
        class Person
        {
            private string name;
     
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
     
            public Person(string name)
            {
                this.name = name;
            }
        }
    }
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
                string b = new string(new char[] { '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 = new Person("jia");
                Person p2 = new Person("jia");
                Console.WriteLine(p1 == p2);
                Console.WriteLine(p1.Equals(p2));
     
     
                Person p3 = new Person("jia");
                Person p4 = p3;
                Console.WriteLine(p3 == p4);
                Console.WriteLine(p3.Equals(p4));
     
                Console.ReadLine();
            }
        }
    }
     
    运行程序,会输出什么呢?
    答案是 truetruefalsetruefalsefalsetruetrue
    为什么会出现这个答案呢?因为值类型是存储在内存中的堆栈(以后简称栈),而引用类型的变量在栈中仅仅是存储引用类型变量的地址,而其本身则存储在堆中。
        ==操作比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量在堆中存储的地址是否相同,即栈中的内容是否相同。
        equals操作表示的两个变量是否是对同一个对象的引用,即堆中的内容是否相同。
        而字符串是一个特殊的引用型类型,在C#语言中,重载了string 对象的很多方法方法(包括equals()方法),使string对象用起来就像是值类型一样。
        因此在上面的例子中,字符串a和字符串b的两个比较是相等的。
        对于object g 和object h 时内存中两个不同的对象,所以在栈中的内容是不相同的,故不相等。而g.equals(h)用的是sting的equals()方法故相等(多太)。如果将字符串a和b作这样的修改:
            string a="aa";
            string b="aa";
    则,g和h的两个比较都是相等的。这是因为系统并没有给字符串b分配内存,只是将"aa"指向了b。所以a和b指向的是同一个字符串(字符串在这种赋值的情况下做了内存的优化)。
    对于p1和p2,也是内存中两个不同的对象,所以在内存中的地址肯定不相同,故p1==p2会返回false,又因为p1和p2又是对不同对象的引用,所以p1.equals(p2)将返回false
    对于p3和p4,p4=p3,p3将对对象的引用赋给了p4,p3和p4是对同一个对象的引用,所以两个比较都返回true
     
    如果我们对person的equals方法重写:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace ConsoleApplication1
    {
        class Person
        {
            private string name;
     
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
     
            public Person(string name)
            {
                this.name = name;
            }
     
            public override bool Equals(object obj)
            {
                if (!(obj is Person))
                    return false;
                Person per = (Person)obj;
                return this.Name == per.Name;
            }
        }
    }
     
    那么p1.equals(p2),就会返回true
  • 相关阅读:
    Leetcode Binary Tree Level Order Traversal
    Leetcode Symmetric Tree
    Leetcode Same Tree
    Leetcode Unique Paths
    Leetcode Populating Next Right Pointers in Each Node
    Leetcode Maximum Depth of Binary Tree
    Leetcode Minimum Path Sum
    Leetcode Merge Two Sorted Lists
    Leetcode Climbing Stairs
    Leetcode Triangle
  • 原文地址:https://www.cnblogs.com/lcyuhe/p/5642492.html
Copyright © 2011-2022 走看看