Overload Operator ==

AIt is derived directly from the class Object, instead of Aor Objectoverload ==, so why the following code does not cause an error:

class Program
{
    static void Main(string[] args)
    {
        A a1 = new A();
        A a2 = new A();

        if (a1 == a2) ... ;
    }
}

class A { }

thank

+3
source share
6 answers

A comes directly from the Object class and is not an A or Object overload == operator, so why doesn't the following code throw an error?

As in the case of your other question, you seem to have a strange belief that there is any overloaded operator, does it have any relation to whether it is possible to choose the operator meaningfully. This is not .

, , , , . , .

. , int, uint, long, ulong, bool, char, float, double, decimal, object, string, , .

, . "A" , NULL, .

.

, , . , , , , . . , , - ; "myString == myException", . "" "", , .

, == " ".

, , user-defined == - , , . , . ?

+6

== ( ) a1 a2. A, a1 == a2 false .

+4

==, , ( ). . .

+3

Object , .

+3

The basic operator ==is invoked so that it does not give any errors.

+2
source

By default, the == operator checks reference equality by determining if two links point to the same object, so reference types are not needed to implement the == operator to get this functionality.

From: http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

An important bit regarding your question:

reference types do not need to execute the == operator to get this functionality

+1
source

All Articles