Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone. I've got one interesting question. There is two realizations of one class:
The first one:
C#
class A
{
    public int Value { get; private set; }

    public A(int value)
    {
        Value = value;
    }
}

And the second:
C#
class A : IEquatable<a>
{
    public int Value { get; private set; }

    public A(int value)
    {
        Value = value;
    }

    public bool Equals(A obj)
    {
        return Value == obj.Value;
    }
}
</a>

The Main method:
C#
static void Main(string[] args)
{
    A[] arr = new A[10];
    List<a> lst = new List<a>();
    for (int i = 0; i < arr.Length; i++)
    {
        arr[i] = new A(i);
        lst.Add(arr[i]);
    }
    foreach (A item in arr)
    {
        if (lst.Contains(item))
        Console.WriteLine("Contains " + item.Value);
    }
}
</a></a>

So my question is: Which realization of class A I have to use?
Posted

The answer depends entirely on your requirements.

The implementation without IEquatable will use reference equality - so will only match the same instances of A.

In your 2nd implementation, you are saying you want value equality - so all objects with the same value of Value will match.

MSDN: Equality Comparisons[^]
 
Share this answer
 
It actually depends. IEquatable is used when you need a custom way of comparing the objects. You could for example have a copy of a data record that has an ID and some other properties. The copy can be updated by the user and when the user is done you want to update the original. You would normally never find the original using the default way if there is a change because the original and new data record are not the same. In such a case you might want to use IEquatable to use only the ID's when doing a compare. You could do this another way but sometimes this is just the most logical. In your case it doesn't seem to matter. But if comparing should only be done with Value, even if new properties are added, then you should use IEquatable.

Good luck!
 
Share this answer
 
In your simple case it would not make any difference. The difference would come in if you had more properties to deal with. Take a case like this:-

C#
class A
   {
       public int Value { get; private set; }
       public string myString { get; private set; }

       public A(int value, string mystring)
       {
           Value = value;
           myString = mystring;
       }
   }

   class A:IEquatable<A>
   {
       public int Value { get; private set; }
       public string myString { get; private set; }

       public A(int value, string mystring)
       {
           Value = value;
           myString = mystring;
       }

       public bool Equals(A other)
       {
           return other.Value == Value;
       }
   }

then if you ran this method :-

C#
private void button1_Click(object sender, EventArgs e)
        {
            A[] arr = new A[10];
            List<A> lst = new List<A>();
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = new A(i, i.ToString());
                lst.Add(new A(i, "A " + i.ToString()));
            }
            foreach (A item in arr)
            {
                if (lst.Contains(item))
                    textBox1.Text += "Contains " + item.Value + "\n";
            }
        }

on the first iteration of A, you would get no results, but if you ran it over the second iteration of A, you would get all the values of A.
Hope this helps
 
Share this answer
 
Comments
Dalek Dave 7-Oct-11 8:15am    
Good Answer
Some extra information to go with the above excellent answers. (Yes, it depends, but based on your test it looks like you want value equality so implement IEquatable.)

This article[^] has some good tips about what else you should do to get value comparison. I tend to think that you should also override != and == to give the value comparison, because it is very strange if a.Equals(b) and a == b give different answers.

Typically you'd do this on either a value type or a data storage class, which you want value type semantics on but don't want to pay for copying all the time
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900