Michał Woliński
Michał Woliński

Reputation: 346

Binary Compare Objects

I need to compare 2 objects of the same class. I was sure that the fastest way will be to read them as number, so (int)Obj1 - (int)Obj2 will give me 0 if they are equal. However, it looks like I cant cast it that way. Do you know how to fast compare objects? I wish to avoid going through all parameters, because there is no need to know where is the difference.

Upvotes: 0

Views: 572

Answers (1)

Kasper Due
Kasper Due

Reputation: 709

If you want to compare if 2 references is from the same object, you can use equals method. If you want to compare the properties from the class with each other, then you need to override the Equals method in your class.

public override bool Equals(object obj)
    {
        if (obj == null)
            return false;
        if (!(obj is Person))
            return false;
        return Name == ((Person) obj).Personer; 

Upvotes: 1

Related Questions