-1

How do i use an objects equals() and the gethashcode() method to check the an objects field value to see if it is true?

for example order numbers

I have several different objects that I am initiating and I need to run an equals() method to check them to see if they are equals.

user2981049
  • 11
  • 1
  • 2

2 Answers2

0

Override both Equals() and GetHasCode() with your own implementation and then you don't have to rely on the default implementation in Object type and you'll have full control over what's going on.

Lost_Cause
  • 96
  • 2
0

If the objects that you are referring to are your own custom classes, then you will need to override the Equals() and GetHashcode() methods.

Example:

class Program
{
    static void Main(string[] args)
    {
        Person p1 = new Person { FirstName = "Manny", Birthday = new DateTime(1970, 1, 4) };
        Person p2 = new Person { FirstName = "Moe", Birthday = new DateTime(1970, 1, 4) };
        Person p3 = new Person { FirstName = "Manny", Birthday = new DateTime(1970, 1, 4) };

        Console.WriteLine("p1 = p2? {0}", p1.Equals(p2));
        Console.WriteLine("p2 = p3? {0}", p2.Equals(p3));
        Console.WriteLine("p1 = p3? {0}", p1.Equals(p3));

        Console.WriteLine("p1 Hash = {0}", p1.GetHashCode());
        Console.WriteLine("p2 Hash = {0}", p2.GetHashCode());
        Console.WriteLine("p3 Hash = {0}", p3.GetHashCode());    
    }
}

class Person
{
    public string FirstName { get; set; }
    public DateTime Birthday { get; set; }

    // You define what an equaled object is. 
    // In this example, we will say a Person is equal if they have the same
    // FirstName and Birthday
    public override bool Equals(object obj)
    {
        bool equals = false;
        if (obj is Person)
        {
            Person p = (Person)obj;
            if (p.FirstName == this.FirstName && p.Birthday == this.Birthday)
                equals = true;
        }
        return equals;
    }

    // You define what the hashcode of the object is. 
    // In this example, we will define the hashcode as the 
    // concatenation of the FirstName and Birthday
    public override int GetHashCode()
    {
        return string.Format("{0}-{1}", this.FirstName, this.Birthday).GetHashCode();
    }
}

The output of the program will be...

p1 = p2? False
p2 = p3? False
p1 = p3? True
p1 Hash = 1727998619
p2 Hash = 1625318294
p3 Hash = 1727998619
Jed
  • 10,649
  • 19
  • 81
  • 125