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. overriding equals

overriding equals

Scheduled Pinned Locked Moved C#
databasetutorialquestion
7 Posts 4 Posters 1 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.
  • G Offline
    G Offline
    Gilbert Consellado
    wrote on last edited by
    #1

    public class User{
    public int UserID {get;set;}
    public string Username {get;set;}

    publict override bool Equals(object obj)
    {
    if(!(obj is User)) return false;
    return (obj as User).UserID == UserID;
    }

    public override int GetHashCode()
    {
    //UserID is form the database
    return UserID;
    }

    public override ToString()
    {
    return Username;
    }
    }

    base on the code above, when i create to object from the database, comparing them will return false even they are the same. example:

    User u1 = new User(){UserID = 1, Username = "FirstUser"};
    User u2 = new User(){UserID = 1, Username = "FirstUser"};

    var b = u1 == u2;

    variable b is false; I thought they will be the same, is there i missing there? I will appreciate for any advice will be given Thank you.

    B 2 Replies Last reply
    0
    • G Gilbert Consellado

      public class User{
      public int UserID {get;set;}
      public string Username {get;set;}

      publict override bool Equals(object obj)
      {
      if(!(obj is User)) return false;
      return (obj as User).UserID == UserID;
      }

      public override int GetHashCode()
      {
      //UserID is form the database
      return UserID;
      }

      public override ToString()
      {
      return Username;
      }
      }

      base on the code above, when i create to object from the database, comparing them will return false even they are the same. example:

      User u1 = new User(){UserID = 1, Username = "FirstUser"};
      User u2 = new User(){UserID = 1, Username = "FirstUser"};

      var b = u1 == u2;

      variable b is false; I thought they will be the same, is there i missing there? I will appreciate for any advice will be given Thank you.

      B Offline
      B Offline
      Brisingr Aerowing
      wrote on last edited by
      #2

      You need to override the == and != operators.

      public static bool operator ==(User lhs, User rhs)
      {
      if(lhs == null && rhs == null) return true;
      if(lhs == null && rhs != null) return false;
      if(lhs != null && rhs == null) return false;
      return lhs.UserID == rhs.UserID;
      }

      public static bool operator !=(User lhs, User rhs)
      {
      return !(lhs == rhs);
      }

      Just insert these into the class and you will be able to use == and != to check for equality and inequality. Note that if you override one, C# forces you to override the other. There are several other operators you can override. You can get all the nitty gritty details on MSDN[^].

      What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

      G Richard DeemingR 2 Replies Last reply
      0
      • G Gilbert Consellado

        public class User{
        public int UserID {get;set;}
        public string Username {get;set;}

        publict override bool Equals(object obj)
        {
        if(!(obj is User)) return false;
        return (obj as User).UserID == UserID;
        }

        public override int GetHashCode()
        {
        //UserID is form the database
        return UserID;
        }

        public override ToString()
        {
        return Username;
        }
        }

        base on the code above, when i create to object from the database, comparing them will return false even they are the same. example:

        User u1 = new User(){UserID = 1, Username = "FirstUser"};
        User u2 = new User(){UserID = 1, Username = "FirstUser"};

        var b = u1 == u2;

        variable b is false; I thought they will be the same, is there i missing there? I will appreciate for any advice will be given Thank you.

        B Offline
        B Offline
        Brisingr Aerowing
        wrote on last edited by
        #3

        You might also want to check for null being passed in to the Equals method. i.e. insert the following line into the beginning of the method:

        if(other == null) return false;

        What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

        1 Reply Last reply
        0
        • B Brisingr Aerowing

          You need to override the == and != operators.

          public static bool operator ==(User lhs, User rhs)
          {
          if(lhs == null && rhs == null) return true;
          if(lhs == null && rhs != null) return false;
          if(lhs != null && rhs == null) return false;
          return lhs.UserID == rhs.UserID;
          }

          public static bool operator !=(User lhs, User rhs)
          {
          return !(lhs == rhs);
          }

          Just insert these into the class and you will be able to use == and != to check for equality and inequality. Note that if you override one, C# forces you to override the other. There are several other operators you can override. You can get all the nitty gritty details on MSDN[^].

          What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

          G Offline
          G Offline
          Gilbert Consellado
          wrote on last edited by
          #4

          thank you for the quick reply. I thought overriding equals will be enough, By the way, one more question base on the code above, which is most efficient using the "Equals" method or the equals operator? Thank you

          F 1 Reply Last reply
          0
          • G Gilbert Consellado

            thank you for the quick reply. I thought overriding equals will be enough, By the way, one more question base on the code above, which is most efficient using the "Equals" method or the equals operator? Thank you

            F Offline
            F Offline
            F ES Sitecore
            wrote on last edited by
            #5

            http://blogs.msdn.com/b/csharpfaq/archive/2004/03/29/when-should-i-use-and-when-should-i-use-equals.aspx[^]

            1 Reply Last reply
            0
            • B Brisingr Aerowing

              You need to override the == and != operators.

              public static bool operator ==(User lhs, User rhs)
              {
              if(lhs == null && rhs == null) return true;
              if(lhs == null && rhs != null) return false;
              if(lhs != null && rhs == null) return false;
              return lhs.UserID == rhs.UserID;
              }

              public static bool operator !=(User lhs, User rhs)
              {
              return !(lhs == rhs);
              }

              Just insert these into the class and you will be able to use == and != to check for equality and inequality. Note that if you override one, C# forces you to override the other. There are several other operators you can override. You can get all the nitty gritty details on MSDN[^].

              What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              As you're dealing with a class, not a struct, you can simplify the operator to:

              public static bool operator ==(User lhs, User rhs)
              {
              return Equals(lhs, rhs);
              }

              public static bool operator !=(User lhs, User rhs)
              {
              return !Equals(lhs, rhs);
              }

              The static Equals method[^] will perform the null checks for you, and then call the instance Equals method on the first parameter:

              public static bool Equals(Object objA, Object objB)
              {
              if (objA==objB) {
              return true;
              }
              if (objA==null || objB==null) {
              return false;
              }
              return objA.Equals(objB);
              }

              At first glance, it might look like the first line of that method would cause a stack overflow, since your == operator is calling a method that calls the == operator. However, within the Equals method, it's calling the == operator on the Object type, which just tests for reference equality.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              G 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                As you're dealing with a class, not a struct, you can simplify the operator to:

                public static bool operator ==(User lhs, User rhs)
                {
                return Equals(lhs, rhs);
                }

                public static bool operator !=(User lhs, User rhs)
                {
                return !Equals(lhs, rhs);
                }

                The static Equals method[^] will perform the null checks for you, and then call the instance Equals method on the first parameter:

                public static bool Equals(Object objA, Object objB)
                {
                if (objA==objB) {
                return true;
                }
                if (objA==null || objB==null) {
                return false;
                }
                return objA.Equals(objB);
                }

                At first glance, it might look like the first line of that method would cause a stack overflow, since your == operator is calling a method that calls the == operator. However, within the Equals method, it's calling the == operator on the Object type, which just tests for reference equality.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                G Offline
                G Offline
                Gilbert Consellado
                wrote on last edited by
                #7

                Thank you, for all the hint, i finally got it now

                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