Vikram
Vikram

Reputation: 1627

Can I Implement GetHashCode for my custom Collection

I have a custom collection as shown below

public class CustomCollection<T>:IEnumerable<T>, IEnumerator<T>
{
    int size = 0;
    int current = 0;
    int position = -1;
    CustomComparer<T> cmp = new CustomComparer<T>();

    T[] collection = null;
    public CustomCollection(int sizeofColl)
    {
        size = sizeofColl;
        collection = new T[size];
    }

    public void Push(T value)
    {
        if (!collection.Contains(value, cmp))
            collection[current++] = value;
    }

    public T Pop()
    {
        return collection[--current];
    }        

    IEnumerator<T> IEnumerable<T>.GetEnumerator()
    {
        return (IEnumerator<T>)this;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    public T Current
    {
        get { return collection[position]; }
    }

    public void Dispose()
    {

    }

    object System.Collections.IEnumerator.Current
    {
        get { throw new NotImplementedException(); }
    }

    public bool MoveNext()
    {
        position++;
        if (position >= collection.Length)
            return false;
        else
            return true;
    }

    public void Reset()
    {
        throw new NotImplementedException();
    }
}

Now I want to have a collection of Person class which is as below along with the IEqualityComparer

 public class Person
{
    public string Name { get; set; }
    public int ID { get; set; }       
}

public class CustomComparer<T>:IEqualityComparer<T>    {


    public bool Equals(T x, T y)
    {
        Person p1 = x as Person;
        Person p2 = y as Person;
        if (p1 == null || p2 == null)
            return false;
        else
            return p1.Name.Equals(p2.Name);
    }

    public int GetHashCode(T obj)
    {
        Person p = obj as Person;
        return p.Name.GetHashCode();
    }
}

Now when I perform the following operation on the collection, why only Equals Method is called and not the GetHashCode() ?

  CustomCollection.CustomCollection<Person> custColl = new CustomCollection<Person>(3);
        custColl.Push(new Person() { Name = "per1", ID = 1 });
        custColl.Push(new Person() { Name = "per2", ID = 2 });
        custColl.Push(new Person() { Name = "per1", ID = 1 });

Or how can I make my code to call GetHashCode ?

Upvotes: 0

Views: 493

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062780

This relates to the line:

if (!collection.Contains(value, cmp))

A test against a vector or sequence (since that looks like Enumerable.Contains) would have no purpose in calling GetHashCode(); that is useful if the data has been grouped into hash-buckets or some other optimized structure, but the data here is just a flat sequence of values. If it needs to call a method, it might as well call Equals rather than GetHashCode(), because if the hash was the same it would still need to call Equals (a hash-code indicates non-equality, but cannot indicate equality). So it is a choice of calling exactly one method per object, vs at least one method per object, and possibly two methods per object. The first is obviously preferable.

If the data were a Dictionary<Person, ...> or a HashSet<Person>, then I would expect GetHashCode() to be used.

Upvotes: 2

Related Questions