Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compare two objects of same class? [duplicate]

Tags:

c#

.net

object

oop

I want to know how can I compare two objects (which is of the same class) like the string.Compare() method.

Is there any way to do this?

like image 246
Pankaj Kumar Avatar asked Dec 24 '13 07:12

Pankaj Kumar


4 Answers

You can implement IComparable interface, like sugested here:

public class Temperature : IComparable 
{
    // The temperature value 
    protected double temperatureF;

    public int CompareTo(object obj) {
        if (obj == null) return 1;

        Temperature otherTemperature = obj as Temperature;
        if (otherTemperature != null) 
            return this.temperatureF.CompareTo(otherTemperature.temperatureF);
        else 
           throw new ArgumentException("Object is not a Temperature");
    }
  ...

source

You will have CompareTo method which will compare items of your class. More on IComparable can be found here on SO. Having CompareTo you can sort lists of your objects according to comparision function like mentioned here

like image 140
Kamil Budziewski Avatar answered Oct 05 '22 10:10

Kamil Budziewski


since objects are reference types, so you should use obj.Equals() method. also note that:

string.ReferenceEquals(str, str2);

It obviously compares references.

str.Equals(str2) 

Tries to compare references at first. Then it tries to compare by value.

str == str2

Does the same as Equals.

like image 31
Zeeshan Avatar answered Oct 05 '22 10:10

Zeeshan


You need to implement the IComparable interface.

private class sortYearAscendingHelper : IComparer
{
   int IComparer.Compare(object a, object b)
   {
      car c1=(car)a;
      car c2=(car)b;
      if (c1.year > c2.year)
         return 1;
      if (c1.year < c2.year)
         return -1;
      else
         return 0;
   }
}

Original post can be found Here

like image 38
Ramashankar Avatar answered Oct 05 '22 12:10

Ramashankar


You can check two equality Reference Equality and Value Equality

Reference equality

Reference equality means that two object references refer to the same underlying object. This can occur through simple assignment, as shown in the following example.

class Test
{
    public int Num { get; set; }
    public string Str { get; set; }

    static void Main()
    {
        Test a = new Test() { Num = 1, Str = "Hi" };
        Test b = new Test() { Num = 1, Str = "Hi" };

        bool areEqual = System.Object.ReferenceEquals(a, b);
        // Output will be false
        System.Console.WriteLine("ReferenceEquals(a, b) = {0}", areEqual);

        // Assign b to a.
        b = a;

        // Repeat calls with different results.
        areEqual = System.Object.ReferenceEquals(a, b);
        // Output will be true
        System.Console.WriteLine("ReferenceEquals(a, b) = {0}", areEqual);


    }
}

Value equality

Value equality means that two objects contain the same value or values. For primitive value types such as int or bool, tests for value equality are straightforward.

like image 26
Kumod Singh Avatar answered Oct 05 '22 10:10

Kumod Singh