0

Given the interfaces:

public interface IStorable<TPk>
{
    TPk ID { get; set; }
    ...
}

And

public interface ICacheHolder<TCached,TPk> where TCached : IStorable<TPk>
{
    ...
    TCached GetValueByID(TPk id);
    ...
}

And the concrete implementation:

public class BasicCacheHolder<TCached, TPk>: ICacheHolder<TCached, TPk> where TCached : IStorable<TPk>
{
    ...
    public TCached GetValueByID(TPk id)
    {
        return CacheEntries.SingleOrDefault(ce => ce.Value.ID == id);
    }
}

I get the error that "Operator '==' cannot be applied to the operands of type 'TPk' and 'TPk', Cannot apply operator '==' to operands of type 'TPk' and 'TPk'"

I assume this is because one of the TPk generic types is hiding the reference to the other TPk, but attempting to rename them appears to break my code.

How do I fix this issue?

EDIT

CachedEntry is a very simple class of the following:

public class CacheEntry<T>
{
    public T Value { get; set; }
    public DateTime RetrievedAt { get; set; }
}
Ranger
  • 1,139
  • 1
  • 13
  • 34
  • What is the type of `CacheEntries`? – D Stanley Feb 04 '16 at 17:07
  • It's a List>. Added CacheEntry to the question. It's a very simple data holder with just two fields; I could have used a tuple, but I wanted the fields to be named. – Ranger Feb 04 '16 at 17:10
  • 1
    See the duplicate. Use `EqualityComparer.Default.Equals(ce.Value.ID, id)` instead. – D Stanley Feb 04 '16 at 17:12
  • 1
    `operator==` needs to be determined compile-time. Since it's definition is `BasicCacheHolder`, there's still no type `TPk` that contains that operator. It cannot assume `TPk : class`, so I'd opt for a duplicate of: http://stackoverflow.com/questions/390900/cant-operator-be-applied-to-generic-types-in-c – Caramiriel Feb 04 '16 at 17:16

0 Answers0