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. Coding Equals Operator

Coding Equals Operator

Scheduled Pinned Locked Moved C#
csharpc++graphicstutorialquestion
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.
  • T Offline
    T Offline
    Tom Moore
    wrote on last edited by
    #1

    Hi, Im just wondering, how do you write the equals operator in C#. Im slightly confused about the syntax. For example in C++ for a vector : class vector{ private: float m_x; float m_y; float m_z; public: vector(x,y,z): m_x(x),m_y(y),m_z(z); vector(): m_x(0),m_y(0),m_z(0); ~vector(); bool operator==(Vector v) { if (this == v) return true; else return (m_x == v.m_x && v.m_y && v.m_z); } } I know its got something to do the Equals Function and Reference Equals Method. How do you test to see if its the same instance? Many Thanks in advance ;) Tom

    N S 2 Replies Last reply
    0
    • T Tom Moore

      Hi, Im just wondering, how do you write the equals operator in C#. Im slightly confused about the syntax. For example in C++ for a vector : class vector{ private: float m_x; float m_y; float m_z; public: vector(x,y,z): m_x(x),m_y(y),m_z(z); vector(): m_x(0),m_y(0),m_z(0); ~vector(); bool operator==(Vector v) { if (this == v) return true; else return (m_x == v.m_x && v.m_y && v.m_z); } } I know its got something to do the Equals Function and Reference Equals Method. How do you test to see if its the same instance? Many Thanks in advance ;) Tom

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      Tom, The following pages from the .NET Framework General Reference should be of help: Guidelines for Implementing Equals and the Equality Operator (==)[^] and Implementing the Equals Method[^]

      - Nick Parker Microsoft MVP - Visual C#
      My Blog | My Articles

      T 1 Reply Last reply
      0
      • N Nick Parker

        Tom, The following pages from the .NET Framework General Reference should be of help: Guidelines for Implementing Equals and the Equality Operator (==)[^] and Implementing the Equals Method[^]

        - Nick Parker Microsoft MVP - Visual C#
        My Blog | My Articles

        T Offline
        T Offline
        Tom Moore
        wrote on last edited by
        #3

        Thanks Nick, Much Appreciated. Its just that in C++ you would use the this statement and check against the type on the right. I was just wondering if ReferenceEquals and Equals use the same kind of technique deep down in the CLR thats all. But as for implementing it, you've hit the nail on the head. Many Thanks :laugh:

        1 Reply Last reply
        0
        • T Tom Moore

          Hi, Im just wondering, how do you write the equals operator in C#. Im slightly confused about the syntax. For example in C++ for a vector : class vector{ private: float m_x; float m_y; float m_z; public: vector(x,y,z): m_x(x),m_y(y),m_z(z); vector(): m_x(0),m_y(0),m_z(0); ~vector(); bool operator==(Vector v) { if (this == v) return true; else return (m_x == v.m_x && v.m_y && v.m_z); } } I know its got something to do the Equals Function and Reference Equals Method. How do you test to see if its the same instance? Many Thanks in advance ;) Tom

          S Offline
          S Offline
          Scott Dorman
          wrote on last edited by
          #4

          You can also check the following thread: Semantics of Object.Equals(object)[^].

          ----------------------------- In just two days, tomorrow will be yesterday.

          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