Difference in C # Equals and the ==

Turn: https: //blog.csdn.net/hujiao199/article/details/4027286

using System;
using System.Collections.Generic;
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;
        }
    }


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

Why the answer is true true false true false false true true


Because the value type is stored in the stack memory (hereinafter referred to as stack), and a reference type variable in the stack address stored only reference type variable, which itself is stored on the heap.
  "==": comparison operation is the value of two variables are equal, the two variables are the same as the address stored in the stack, i.e. the stack is the same as the contents of a reference to variables represented.
  "equals": two variables indicate whether the operation is a reference to the same object, that is, the contents of the heap are the same.
    And the string is a special type of reference type, the C # language, many overloaded methods of string objects (including equals () method), so it is like a string object as a value type.
    Therefore, in the above example, the first output pair, two strings and a string comparison is equal to b.
    For the second pair of output object g = a and object h = b, in memory of two different objects, so that the contents of the stack are not the same, it is not equal. And g.equals (h) of using a sting equals () method so that equal (multiple too). If the strings a and b for such modifications:
        String = a "AA";
        String b = "AA";
then, g and h of the two comparators are equal. This is because the system does not allocate memory for strings b, just "aa" points to b. So points a and b are the same string (in the case of this optimized assignment of memory).
For p1 and p2, but also in memory of two different objects, the address in memory is certainly not the same, so p1 == p2 returns false, and because p1 and p2 is a reference to the different objects, so p1. equals (p2) returns false.
For p3 and p4, p4 = p3, p3 a reference to the object are assigned a p4, p3 and p4 is a reference to the same object, so both return relatively true.

Guess you like

Origin www.cnblogs.com/zwj-199306231519/p/11619513.html