C basics: analysis of the difference between Equals of and the operator ==

  • 2020-05-12 03:13:31
  • OfStack

For value types, the equality operator (==) returns true if the object's values are equal, or false if not. For reference types other than string, if two objects reference the same object, == returns true. For the string type, == compares the value of the string.
The == operation compares whether the values of two variables are equal.
The equals () method compares the contents of two objects. equals is about comparing whether a reference type is a reference to the same object.
The comparison of value types is not described here, but the comparison of reference types is discussed below:
First let's look at a program

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

Run the program, what will it output?
The answer is true, true, false, true, false, false, true, true.
Why does this answer appear? Because the value type is a stack stored in memory (hereafter referred to as a stack), the variable of the reference type in the stack is simply the address where the reference type variable is stored, and it itself is stored in the heap.
The == operation compares whether the values of two variables are equal. For referential variables, it indicates whether the two variables have the same address stored in the heap, 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 contents in the heap are the same.
The string is a special reference type, and the C# language overloads many methods of the string object (including the equals() method), making the string object behave like a value type 1.
So in the above example, the two comparisons of the string a and the string b are equal.
For object g and object h, there are two different objects in memory, so the contents in the stack are not the same, so they are not equal. g.equals (h) USES the equals() method of sting. If the strings a and b are modified as follows:
string a="aa";
string b="aa";
Then, both comparisons of g and h are equal. This is because the system does not allocate memory to the string b, it just points "aa" to b. So a and b refer to the same string (the string is optimized for memory in the case of this assignment).
For p1 and p2, they are also two different objects in memory, so the addresses in memory must be different, so p1==p2 will return false, and because p1 and p2 are references to different objects, p1.equals (p2) will return false.
For p3 and p4, p4=p3, p3 assigns references to objects to p4, p3 and p4 are references to the same object, so both comparisons return true.
If we rewrite person's equals method:

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;
        }
    }
}

So p1.equals (p2) will return true.

Related articles: