Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Implementing IEquatable<T>.Equal<T>

I have a class like this:

public class Foo<T> : IEquatable<T> where T : struct
{
    List<T> lst;
    [Other irrelevant member stuff]
}

I want to implement the IEquatable<T> interface for the Foo class. What do I need to do. For simplicity I want to just check whether the List members are equal.

Thanks.

C# 4.0 supported answers are allowable.

Update: Here is what I currently have:

public bool Equals(Foo<T> foo)
{
    return lst.Equals(foo.lst);
}

public override bool Equals(Object obj)
{
    if (obj == null) return base.Equals(obj);

    if (!(obj is Foo<T>))
    {
        throw new Exception("The 'obj' argument is not a Foo<T> object.");
    }
    else
    {
            return Equals(obj as Foo<T>)
    }
}    

public override int GetHashCode()
{
    return this.lst.GetHashCode();
}

public static bool operator ==(Foo<T> f1, Foo<T> f2)
{
   return f1.Equals(f2);
}

public static bool operator !=(Foo<T> f1, Foo<T> f2)
{
   return (!f1.Equals(f2));
}

I get this error:

Error 1 'Foo<T>' does not implement interface member 'System.IEquatable<T>.Equals(T)
like image 357
Chris Avatar asked Jan 17 '10 08:01

Chris


2 Answers

Unfortunately, List<T> doesn't override Equals or GetHashCode. That means even when you've corrected your class declaration, you'll need to perform the comparisons yourself:

public bool Equals(Foo<T> foo)
{
    // These need to be calls to ReferenceEquals if you are overloading ==
    if (foo == null)
    {
        return false;
    }
    if (foo == this)
    {
        return true;
    }
    // I'll assume the lists can never be null
    if (lst.Count != foo.lst.Count)
    {
        return false;
    }
    for (int i = 0; i < lst.Count; i++)
    {
        if (!lst[i].Equals(foo.lst[i]))
        {
            return false;
        }
    }
    return true;
}

public override int GetHashCode()
{
    int hash = 17;
    foreach (T item in lst)
    {
        hash = hash * 31 + item.GetHashCode();
    }
    return hash;
}

public override bool Equals(Object obj)
{
    // Note that Equals *shouldn't* throw an exception when compared
    // with an object of the wrong type
    return Equals(obj as Foo<T>);
}

I would personally think very carefully before overloading == and != too. If you do decide to implement them, you should think about cases where either or both of the values is null:

public static bool operator ==(Foo<T> f1, Foo<T> f2)
{
   if (object.ReferenceEquals(f1, f2))
   {
       return true;
   }
   if (object.ReferenceEquals(f1, null)) // f2=null is covered by Equals
   {
       return false;
   }
   return f1.Equals(f2);
}
like image 95
Jon Skeet Avatar answered Sep 29 '22 09:09

Jon Skeet


Try this.

    public class Foo<T> : IEquatable<Foo<T>> where T : struct
    {
        List<T> lst;

        #region IEquatable<T> Members

        public bool Equals(Foo<T> other)
        {
            if (lst.Count != other.lst.Count)
            {
                return false;
            }

            for (int i = 0; i < lst.Count; i++)
            {
                if (!lst[i].Equals(other.lst[i]))
                {
                    return false;
                }
            }
            return true;
        }

        #endregion

        public override bool Equals(object obj)
        {
            var other = obj as Foo<T>;
            return other != null && Equals(other);
        }


    }
like image 27
Manish Basantani Avatar answered Sep 29 '22 10:09

Manish Basantani