locked
using IEquatable RRS feed

  • Question

  • User527076549 posted
     public class Person : IEquatable<Person>
        {
            public string Name { get; set; }
            public int Id { get; set; }
           
            public bool Equals(Person other)
            {
                if(this.Id==other.Id)
                {
                    return true;
                }
    
                else if(this.Name.Equals(other.Name))
                {
                    return true;
                }
                else if(Object.Equals(this.Name,other.Name))
                {
                    return true;
                }
    
                return false;
            }
        }

    which one is correct Objecct.Equals() or this.Name.Equals() both give same answers

    Thanks you.really appreciate your help 

    Saturday, September 21, 2019 12:22 PM

All replies

  • User-719153870 posted

    Hi amithashenoy,

    this.Name.Equals(other.Name) is String.Equals Method and Object.Equals(this.Name,other.Name) is Object.Equals Method, they are actually the same thing since string inherits from object.

    Popular speaking, this.Name.Equals(other.Name) is like saying "whether A equal to B" while Object.Equals(this.Name,other.Name) is like saying "whether A and B are equal".

    amithashenoy

    which one is correct Objecct.Equals() or this.Name.Equals() both give same answers

    In general, both of them are correct.

    Best Regard,

    Yang Shen

    Monday, September 23, 2019 7:15 AM