Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Are theses the same: object.Equal(obj,null) and obj == null?

Are theses the same: object.Equal(obj,null) and obj == null?

Scheduled Pinned Locked Moved C#
questiondesign
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Christopher Stratmann
    wrote on last edited by
    #1

    Is one better than the other? I have done some research and found that if I have a ClassA that overloads the '==' operator then using ClassA == null will actually go into that overload operator function, but object.Equal(ClassA,null) does not go into the overloaded '==' operator function. Furthermore, what is really weird is if I say ClassA myClassA; in my code and do not set it. myClassA equals null and then coding if (myClassA == null);. My overloaded '==' operator function will get called and I HAVE to use object.Equal(myClassA,null). I cannot use if (this == null && myClassA == null) return true; within my overloaded '==' operator function because it would go into an infinite loop. Another thing which I think is weird is using object.Equal() instead of ==, I will sometimes get a Code Analysis warning CA1062 (Ex: Microsoft.Design : Validate parameter 'value' passed to externally visible method GettingControlEventArgs.set_Control(Control):Void. So in my code if I do not want the overloaded == operator to get called if myClassA is null then I first must ust object.Equal() and then use == to test if myClassA is null to get rid of the Code Analysis warning. Chris

    G 1 Reply Last reply
    0
    • C Christopher Stratmann

      Is one better than the other? I have done some research and found that if I have a ClassA that overloads the '==' operator then using ClassA == null will actually go into that overload operator function, but object.Equal(ClassA,null) does not go into the overloaded '==' operator function. Furthermore, what is really weird is if I say ClassA myClassA; in my code and do not set it. myClassA equals null and then coding if (myClassA == null);. My overloaded '==' operator function will get called and I HAVE to use object.Equal(myClassA,null). I cannot use if (this == null && myClassA == null) return true; within my overloaded '==' operator function because it would go into an infinite loop. Another thing which I think is weird is using object.Equal() instead of ==, I will sometimes get a Code Analysis warning CA1062 (Ex: Microsoft.Design : Validate parameter 'value' passed to externally visible method GettingControlEventArgs.set_Control(Control):Void. So in my code if I do not want the overloaded == operator to get called if myClassA is null then I first must ust object.Equal() and then use == to test if myClassA is null to get rid of the Code Analysis warning. Chris

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      chris175 wrote:

      Is one better than the other?

      The better one is the one that does exactly what you want to do...

      if I have a ClassA that overloads the '==' operator then using ClassA == null will actually go into that overload operator function, but object.Equal(ClassA,null) does not go into the overloaded '==' operator function.

      You are not talking about overloading, but overriding. Overloading is when you have more than one method with the same name but with different parameters. Both the == operator and the Equals method can be overridden, and using either on the object will use the overridden one. If you override the == operator, you should also override the Equals method (and also the != operator and the GetHashCode method). If you use Object.Equals, you have specified that it should use the static method in the Object class. Static methods can not be overridden.

      Furthermore, what is really weird is if I say ClassA myClassA; in my code and do not set it. myClassA equals null

      Only if you declare the variable in a class. If you declare it as a local variable in a method, it will be undefined, and the compiler will prevent you from using it before assigning it a value.

      and then coding if (myClassA == null);. My overloaded '==' operator function will get called and I HAVE to use object.Equal(myClassA,null).

      No, you don't HAVE to. You can simply cast your object to Object: if ((object)myClassA == null) This will use the == operator of the Object class instead of the overridden == operator.

      --- single minded; short sighted; long gone;

      S 1 Reply Last reply
      0
      • G Guffa

        chris175 wrote:

        Is one better than the other?

        The better one is the one that does exactly what you want to do...

        if I have a ClassA that overloads the '==' operator then using ClassA == null will actually go into that overload operator function, but object.Equal(ClassA,null) does not go into the overloaded '==' operator function.

        You are not talking about overloading, but overriding. Overloading is when you have more than one method with the same name but with different parameters. Both the == operator and the Equals method can be overridden, and using either on the object will use the overridden one. If you override the == operator, you should also override the Equals method (and also the != operator and the GetHashCode method). If you use Object.Equals, you have specified that it should use the static method in the Object class. Static methods can not be overridden.

        Furthermore, what is really weird is if I say ClassA myClassA; in my code and do not set it. myClassA equals null

        Only if you declare the variable in a class. If you declare it as a local variable in a method, it will be undefined, and the compiler will prevent you from using it before assigning it a value.

        and then coding if (myClassA == null);. My overloaded '==' operator function will get called and I HAVE to use object.Equal(myClassA,null).

        No, you don't HAVE to. You can simply cast your object to Object: if ((object)myClassA == null) This will use the == operator of the Object class instead of the overridden == operator.

        --- single minded; short sighted; long gone;

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        Guffa wrote:

        You are not talking about overloading, but overriding

        But the custom operator implementation must be a static method, which doesn't fit in with overriding. Even MSDN talks about overloading[^] operators, not overriding them.

        Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

        G 1 Reply Last reply
        0
        • S S Senthil Kumar

          Guffa wrote:

          You are not talking about overloading, but overriding

          But the custom operator implementation must be a static method, which doesn't fit in with overriding. Even MSDN talks about overloading[^] operators, not overriding them.

          Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Yes, you are right. Operators are overloaded. This is a special form of overloading, though, as operators are overloaded on a global level, not within the same class. I didn't really realise that distinction until now. :)

          --- single minded; short sighted; long gone;

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups