Search code examples
c#genericsstaticicomparableicomparer

C# generic class and EqualityComparer


Could anyone explain me what's wrong in the following class declaration:

private class PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType> :
        IComparer<PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType>>
            where TPriorityValue : IComparable
            where IIdentifiableEntry : Identifier<IType>
    {
        public TPriorityValue Priority{get;private set;}
        public IIdentifiableEntry Entry{get;private set;}

        public PriorityQueueEntry(TPriorityValue val,IIdentifiableEntry entry)
        {
            Priority = val;
            Entry = entry;
        }
        public int Compare(PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType> first, PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType> second) 
        {
            if (first.Priority.CompareTo(second.Priority) < 0)
            {
                return -1;
            }
            else if (first.Priority.CompareTo(second.Priority) > 0)
            {
                return 1;
            }
            return EqualityComparer<IIdentifiableEntry>.Default.Equals( first.Entry.Id, second.Entry.Id);
        }
    }

The compiler complaing on the line where I use EqualityComparer. The error is the following:

error CS0176: Static member `object.Equals(object, object)' cannot be accessed with an instance reference, qualify it with a type name instead

I can't see where I'm using an instance reference.


Sorry, my fault. I posted an incomplete question. Just for completeness, Idetifier class is just the following:

public interface Identifier<ID_TYPE> 
{
    ID_TYPE Id{get;set;}
}

using EqualityComparer there, was due to a copy and paste mistake(sorry guys, too much generic code today).

Of course my question was misposed, because I didn't give you all the elements you needed to answer (I'll remove it soon). I needed IType to be IConvertible. Thanks to all anyway.


Solution

  • Finally I solved this way:

    private class PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType> :
            IComparer<PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType>>
                where TPriorityValue : IComparable
                where IIdentifiableEntry : Identifier<IType>
                where IType : IConvertible
        {
            public TPriorityValue Priority{get;private set;}
            public IIdentifiableEntry Entry{get;private set;}
    
            public PriorityQueueEntry(TPriorityValue val,IIdentifiableEntry entry)
            {
                Priority = val;
                Entry = entry;
            }
            public int Compare(PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType> first, PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType> second) 
            {
                if (first.Priority.CompareTo(second.Priority) < 0)
                {
                    return -1;
                }
                else if (first.Priority.CompareTo(second.Priority) > 0)
                {
                    return 1;
                }
                return first.Entry.Id.ToUInt32(NumberFormatInfo.CurrentInfo) < first.Entry.Id.ToUInt32(NumberFormatInfo.CurrentInfo) ? -1:1; 
            }
        }