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
  1. Home
  2. General Programming
  3. C#
  4. error-defines operator == or operator != but does not override Object.GetHashCode()

error-defines operator == or operator != but does not override Object.GetHashCode()

Scheduled Pinned Locked Moved C#
helpcsharpvisual-studiographics
6 Posts 5 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.
  • S Offline
    S Offline
    sasire18
    wrote on last edited by
    #1

    hi I am having strange problem in my class file.I am adding collection to support vector struct but it gives me these two warnings

    C:\Visual Studio Projects\Cons12\EntryPoint.cs(24): 'Cons12.VectorClass.Vector' defines operator == or operator != but does not override Object.Equals(object o))
    C:\Visual Studio Projects\Cons12\EntryPoint.cs(24): 'Cons12.VectorClass.Vector' defines operator == or operator != but does not override Object.GetHashCode()

    I am unable to understand what it is and how it can be resolved Can anyone explain me how and why it happens the above error thanks in advance sasi

    I J G J 4 Replies Last reply
    0
    • S sasire18

      hi I am having strange problem in my class file.I am adding collection to support vector struct but it gives me these two warnings

      C:\Visual Studio Projects\Cons12\EntryPoint.cs(24): 'Cons12.VectorClass.Vector' defines operator == or operator != but does not override Object.Equals(object o))
      C:\Visual Studio Projects\Cons12\EntryPoint.cs(24): 'Cons12.VectorClass.Vector' defines operator == or operator != but does not override Object.GetHashCode()

      I am unable to understand what it is and how it can be resolved Can anyone explain me how and why it happens the above error thanks in advance sasi

      I Offline
      I Offline
      Ingo
      wrote on last edited by
      #2

      Did you used the overwrite word in the definition of the new operator? ------------------------------ A bug in a Microsoft Product? No! It's not a bug it's an undocumented feature!

      S 1 Reply Last reply
      0
      • I Ingo

        Did you used the overwrite word in the definition of the new operator? ------------------------------ A bug in a Microsoft Product? No! It's not a bug it's an undocumented feature!

        S Offline
        S Offline
        sasire18
        wrote on last edited by
        #3

        yes i have used override method its give me output but with the above mentioned two errors what causing it exactly any idea sasi

        1 Reply Last reply
        0
        • S sasire18

          hi I am having strange problem in my class file.I am adding collection to support vector struct but it gives me these two warnings

          C:\Visual Studio Projects\Cons12\EntryPoint.cs(24): 'Cons12.VectorClass.Vector' defines operator == or operator != but does not override Object.Equals(object o))
          C:\Visual Studio Projects\Cons12\EntryPoint.cs(24): 'Cons12.VectorClass.Vector' defines operator == or operator != but does not override Object.GetHashCode()

          I am unable to understand what it is and how it can be resolved Can anyone explain me how and why it happens the above error thanks in advance sasi

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          These warnings are telling you that because you have overriden the == and != operator there are 2 further methods inherited from Object that you should override. Equals - returns true if the object passed into the obj parameter should be considered equal the current object GetHashCode - returns an int used in hashing algorithms. Objects that return true from Equals should return the same HashCode from this method. You can override these methods in your object by using the override keyword.

          1 Reply Last reply
          0
          • S sasire18

            hi I am having strange problem in my class file.I am adding collection to support vector struct but it gives me these two warnings

            C:\Visual Studio Projects\Cons12\EntryPoint.cs(24): 'Cons12.VectorClass.Vector' defines operator == or operator != but does not override Object.Equals(object o))
            C:\Visual Studio Projects\Cons12\EntryPoint.cs(24): 'Cons12.VectorClass.Vector' defines operator == or operator != but does not override Object.GetHashCode()

            I am unable to understand what it is and how it can be resolved Can anyone explain me how and why it happens the above error thanks in advance sasi

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

            Are you sure that they are errors, not warnings? The compiler says that you have created a class that will behave erratically. The Equals method should give the same result as the equality operator, so if you override one of them, you should override the other. If you compare the object to something, it will first look for a specific equality operator for the data type, and if none exists, it uses the Equals method. If you have overridden only the equality operator but not the Equals method, the comparison will work differently depending on what you compare it to. The GetHashCode method is used among other things when you put an object in certain collections. If the GetHashCode methods isn't based on the same data as the equality operator, the object won't work correctly in the collection. --- b { font-weight: normal; }

            1 Reply Last reply
            0
            • S sasire18

              hi I am having strange problem in my class file.I am adding collection to support vector struct but it gives me these two warnings

              C:\Visual Studio Projects\Cons12\EntryPoint.cs(24): 'Cons12.VectorClass.Vector' defines operator == or operator != but does not override Object.Equals(object o))
              C:\Visual Studio Projects\Cons12\EntryPoint.cs(24): 'Cons12.VectorClass.Vector' defines operator == or operator != but does not override Object.GetHashCode()

              I am unable to understand what it is and how it can be resolved Can anyone explain me how and why it happens the above error thanks in advance sasi

              J Offline
              J Offline
              jinzhecheng
              wrote on last edited by
              #6

              It must be a warning ! simply ignore it or add Equals, GetHash function

              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