SoFunction
Updated on 2025-03-07

C# basics: Analysis of the difference between Equals and operator ==

For value types, the equality operator (==) returns true if the values ​​of the object are equal, otherwise false. For reference types other than string, if two objects refer to the same object, == returns true. For string type, == compares the value of the string.
==The operation compares whether the values ​​of the two variables are equal.
The equals() method compares whether the contents of the two objects are consistent. equals means whether the reference type is a reference to the same object.
For comparison of value types, we will not describe it here. The comparison of reference types will be discussed below:
First, let's look at a program
Copy the codeThe code is as follows:

using System;
using ;
using ;
using ;
namespace ConsoleApplication1
{
    class Person
    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public Person(string name)
        {
            = name;
        }
    }
}

Copy the codeThe code is as follows:

using System;
using ;
using ;
using ;
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' });
            (a == b);
            ((b));
            object g = a;
            object h = b;
            (g == h);
            ((h));
            Person p1 = new Person("jia");
            Person p2 = new Person("jia");
            (p1 == p2);
            ((p2));
            Person p3 = new Person("jia");
            Person p4 = p3;
            (p3 == p4);
            ((p4));
            ();
        }
    }
}

What will be output when running the program?
The answer is true, true, false, true, false, false, false, true, true.
Why does this answer appear? Because the value type is a stack stored in memory (hereinafter referred to as the stack), and the variable of the reference type is only the address of the reference type variable in the stack, and itself is stored in the heap.
==The operation compares whether the values ​​of the two variables are equal. For reference variables, it indicates whether the addresses stored in the heap are the same, that is, whether the contents in the stack are the same.
Whether the two variables represented by the equals operation are references to the same object, that is, whether the content in the heap is the same.
Strings are a special reference type. In C# language, many methods of string objects (including equals() method) are overloaded to make string objects use like value types.
Therefore, in the above example, the two comparisons of string a and string b are equal.
For two different objects in memory when object g and object h, the contents in the stack are different, so they are not equal. And (h) uses the equals() method of sing, so it is equal (many too). If you modify the strings a and b like this:
        string a="aa";
        string b="aa";
Then, the two comparisons of g and h are equal. This is because the system does not allocate memory to the string b, but points "aa" to b. So a and b point to the same string (the string is optimized in memory under this kind of assignment).
For p1 and p2, they are also two different objects in memory, so the addresses in memory are definitely different, so p1==p2 will return false, and because p1 and p2 are references to different objects, (p2) will return false.
For p3 and p4, p4=p3, p3 assigns a reference to the object to p4, p3 and p4 are references to the same object, so both comparisons return true.
If we rewrite the equals method of person:
Copy the codeThe code is as follows:

using System;
using ;
using ;
using ;
namespace ConsoleApplication1
{
    class Person
    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public Person(string name)
        {
            = name;
        }
        public override bool Equals(object obj)
        {
            if (!(obj is Person))
                return false;
            Person per = (Person)obj;
            return == ;
        }
    }
}

Then (p2), it will return true.