Difference, == and Object.Equals

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?=

    #1

    Difference, == and Object.Equals

    What is the difference of == and Object.Equals()

    If I want to query werther to pointers are pointing to same instance, do I
    use == or Objects.Equals?

    foo a,b;

    if ( a == b ) ... or

    if (a.Equals(b))



  • Jeroen

    #2
    Re: Difference, == and Object.Equals

    foo a,b;
    >
    if ( a == b ) ... or
    >
    if (a.Equals(b))
    In this example, the first statement will check wether 'a' and 'b'
    refer to the same foo instance.

    The second statement will call the 'Equals' method of foo if
    overridden, or the object 'Equals' method. Typically you may check if
    several/all fields of a and b have the same value. If the foo class
    overrides the equals method, it should (according to msdn) guarantee
    some things:

    - x.Equals(x) returns true.
    - x.Equals(y) returns the same value as y.Equals(x).
    - if (x.Equals(y) && y.Equals(z)) returns true, then x.Equals(z)
    returns true.
    - Successive invocations of x.Equals(y) return the same value as long
    as the objects referenced by x and y are not modified.
    - x.Equals(null) returns false.

    Comment

    • Miroslav Stampar [MCSD.NET / Security+]

      #3
      Re: Difference, == and Object.Equals

      Sometimes Equals method in custom classes is overriden, so for your
      purpose always use object.Equals(o bjA, objB). It always checks if objA
      and objB refer to the same object.

      HTH :)

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Difference, == and Object.Equals

        On Jun 8, 1:05 pm, "Miroslav Stampar [MCSD.NET / Security+]"
        <miroslav.stam. ..@gmail.comwro te:
        Sometimes Equals method in custom classes is overriden, so for your
        purpose always use object.Equals(o bjA, objB). It always checks if objA
        and objB refer to the same object.
        No it doesn't. It will call the overridden Equals method if both objA
        and objB are non-null.

        However, object.Referenc eEquals(objA, objB) *will* always check
        references, regardless of whether Equals and == are overridden/
        overloaded.

        Jon

        Comment

        • Larry Smith

          #5
          Re: Difference, == and Object.Equals

          http://msdn2.microsoft.com/en-us/lib...47(vs.80).aspx


          Comment

          • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

            #6
            Re: Difference, == and Object.Equals

            Miroslav Stampar [MCSD.NET / Security+] wrote:
            Sometimes Equals method in custom classes is overriden, so for your
            purpose always use object.Equals(o bjA, objB). It always checks if objA
            and objB refer to the same object.
            That is Java.

            In C# == can be overridden too and I would consider it best
            practice to override both Equals and ==.

            Arne

            Comment

            • =?ISO-8859-1?Q?G=F6ran_Andersson?=

              #7
              Re: Difference, == and Object.Equals

              Jon Skeet [C# MVP] wrote:
              On Jun 8, 1:05 pm, "Miroslav Stampar [MCSD.NET / Security+]"
              <miroslav.stam. ..@gmail.comwro te:
              >Sometimes Equals method in custom classes is overriden, so for your
              >purpose always use object.Equals(o bjA, objB). It always checks if objA
              >and objB refer to the same object.
              >
              No it doesn't. It will call the overridden Equals method if both objA
              and objB are non-null.
              Yes, it does. The Object.Equals(o bject, object) method is a static
              method in the Object class. Static methods can not be overridden.
              However, object.Referenc eEquals(objA, objB) *will* always check
              references, regardless of whether Equals and == are overridden/
              overloaded.
              >
              Jon
              >

              --
              Göran Andersson
              _____
              Göran Anderssons privata hemsida.

              Comment

              • Larry Smith

                #8
                Re: Difference, == and Object.Equals

                That is Java.
                >
                In C# == can be overridden too and I would consider it best
                practice to override both Equals and ==.
                Not according to MSFT who specifically advises against overriding == for
                non-immutable types.


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: =?ISO-8859-1?Q?Difference, _=3D=3D_and_Obj ect.Equals?=

                  Göran Andersson <guffa@guffa.co mwrote:
                  Jon Skeet [C# MVP] wrote:
                  On Jun 8, 1:05 pm, "Miroslav Stampar [MCSD.NET / Security+]"
                  <miroslav.stam. ..@gmail.comwro te:
                  Sometimes Equals method in custom classes is overriden, so for your
                  purpose always use object.Equals(o bjA, objB). It always checks if objA
                  and objB refer to the same object.
                  No it doesn't. It will call the overridden Equals method if both objA
                  and objB are non-null.
                  Yes, it does. The Object.Equals(o bject, object) method is a static
                  method in the Object class. Static methods can not be overridden.
                  But static methods can call overriden methods themselves, which is
                  exactly what the static Equals method does - as I said before.

                  Put it this way: if object.Equals(o bject, object) doesn't call the
                  overridden Equals method, and only determines reference equality,
                  please explain why the following program prints False then True:

                  using System;
                  using System.Text;

                  class Test
                  {
                  static void Main()
                  {
                  string a = new StringBuilder(" Hello").ToStrin g();
                  string b = new StringBuilder(" Hello").ToStrin g();

                  Console.WriteLi ne (object.Referen ceEquals(a,b));
                  Console.WriteLi ne (object.Equals( a,b));
                  }
                  }

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                  If replying to the group, please do not mail me too

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: =?ISO-8859-1?Q?Difference, _=3D=3D_and_Obj ect.Equals?=

                    Arne Vajhøj <arne@vajhoej.d kwrote:
                    Miroslav Stampar [MCSD.NET / Security+] wrote:
                    Sometimes Equals method in custom classes is overriden, so for your
                    purpose always use object.Equals(o bjA, objB). It always checks if objA
                    and objB refer to the same object.
                    That is Java.

                    In C# == can be overridden too and I would consider it best
                    practice to override both Equals and ==.
                    Just to be picky, == can't be overridden - it can be overloaded. The
                    difference is important. For instance, if we do:

                    using System;
                    using System.Text;

                    class Test
                    {
                    static void Main()
                    {
                    object a = new StringBuilder(" Hello").ToStrin g();
                    object b = new StringBuilder(" Hello").ToStrin g();

                    Console.WriteLi ne(a==b);
                    }
                    }

                    that will print False, because ==(object,objec t) is used instead of
                    the ==(string,strin g) *overloaded* which is provided by the string
                    class - whereas a.Equals(b) will print True because the Equals method
                    is *overridden*.

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                    If replying to the group, please do not mail me too

                    Comment

                    • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                      #11
                      Re: Difference, == and Object.Equals

                      Jon Skeet [C# MVP] wrote:
                      Arne Vajhøj <arne@vajhoej.d kwrote:
                      >In C# == can be overridden too and I would consider it best
                      >practice to override both Equals and ==.
                      >
                      Just to be picky, == can't be overridden - it can be overloaded. The
                      difference is important.
                      Overloaded would be the correct C# terminology.

                      :-)

                      Arne

                      Comment

                      • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                        #12
                        Re: Difference, == and Object.Equals

                        Larry Smith wrote:
                        >That is Java.
                        >>
                        >In C# == can be overridden too and I would consider it best
                        >practice to override both Equals and ==.
                        >
                        Not according to MSFT who specifically advises against overriding == for
                        non-immutable types.
                        True.

                        But f.ex. FxCop docs state:

                        "The equality operator is intended to be a syntactically convenient way
                        of accessing the functionality of the Equals method."

                        Arne


                        Comment

                        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                          #13
                          Re: Difference, == and Object.Equals

                          Jon Skeet [C# MVP] wrote:
                          Göran Andersson <guffa@guffa.co mwrote:
                          >Jon Skeet [C# MVP] wrote:
                          >>On Jun 8, 1:05 pm, "Miroslav Stampar [MCSD.NET / Security+]"
                          >><miroslav.sta m...@gmail.comw rote:
                          >>>Sometimes Equals method in custom classes is overriden, so for your
                          >>>purpose always use object.Equals(o bjA, objB). It always checks if objA
                          >>>and objB refer to the same object.
                          >>No it doesn't. It will call the overridden Equals method if both objA
                          >>and objB are non-null.
                          >Yes, it does. The Object.Equals(o bject, object) method is a static
                          >method in the Object class. Static methods can not be overridden.
                          >
                          But static methods can call overriden methods themselves, which is
                          exactly what the static Equals method does - as I said before.
                          I see. So by "it", you didn't mean the call, but the method. The call
                          goes to the static method, but if you say that the method in turn calls
                          a virtual method in certain cases, I take your word for it.


                          --
                          Göran Andersson
                          _____
                          Göran Anderssons privata hemsida.

                          Comment

                          • Jon Skeet [C# MVP]

                            #14
                            Re: =?ISO-8859-1?Q?Difference, _=3D=3D_and_Obj ect.Equals?=

                            Göran Andersson <guffa@guffa.co mwrote:
                            But static methods can call overriden methods themselves, which is
                            exactly what the static Equals method does - as I said before.
                            I see. So by "it", you didn't mean the call, but the method.
                            Exactly - which is what I understood Miroslav to mean as well; if "it"
                            is the compiler in his post, then no method is involved in the first
                            place.
                            The call goes to the static method, but if you say that the method in
                            turn calls a virtual method in certain cases, I take your word for
                            it.
                            In all cases where neither argument is null.

                            --
                            Jon Skeet - <skeet@pobox.co m>
                            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                            If replying to the group, please do not mail me too

                            Comment

                            • Christof Nordiek

                              #15
                              Re: Difference, == and Object.Equals

                              "Jon Skeet [C# MVP]" <skeet@pobox.co mschrieb
                              No it doesn't. It will call the overridden Equals method if both objA
                              and objB are non-null.
                              from the Docs of Object.Equals(O bject, Object):

                              true if objA is the same instance as objB or if both are null references or
                              if objA.Equals(obj B) returns true; otherwise, false

                              From this objA.Equals(obj B) will be called also if objB is null.
                              Also, if both are the same object, Equals wouldn't be called. But this only
                              makes a difference if Equals is implented badly in the type of objA.

                              Christof


                              Comment

                              Working...