C#: Default implementation for == and != operators for objects

前端 未结 3 2059
遥遥无期
遥遥无期 2020-12-06 00:17

I\'d like to know what is default implementation for equality operatort (== and !=)

Is it?

public static bool operator ==(object obj1, object obj2)
{         


        
3条回答
  •  情深已故
    2020-12-06 01:05

    The C# language specification, Section 7.9 covers the exact behavior of the built-in == operator. For example, when using == on reference-type values, the following section applies:

    7.9.6 Reference type equality operators

    The predefined reference type equality operators are:

    bool operator ==(object x, object y);
    bool operator !=(object x, object y);

    The operators return the result of comparing the two references for equality or non-equality.

    Since the predefined reference type equality operators accept operands of type object, they apply to all types that do not declare applicable operator == and operator != members. Conversely, any applicable user-defined equality operators effectively hide the predefined reference type equality operators.

    [More details skipped...]

    Note that "comparing two references for equality" does not mean "the result of calling obj1.Equals(obj2)". It means that both references must point to the same object (reference equality).

提交回复
热议问题