2

I've got a class with two generics and two indexers (such that t1->t2 and t2->t1.)

public class Mapper<T1,T2>
{
    private readonly Dictionary<T1,T2> _forward;
    private readonly Dictionary<T2,T1> _reverse;

    public T2 this[T1 val] 
    {
        get => _forward[val];
        set => _reverse[value] = val;
    }

    public T1 this[T2 val] 
    {
        get => _reverse[val];
        set => _forward[value] = val;
    }
}

Obviously if I tried to use a Mapper<char,char> the compiler would complain because it couldn't decide which indexer to use. How do I indicate to the compiler (and the reader) that typeof(T1)!=typeof(T2)?

Jakob Lovern
  • 1,301
  • 7
  • 24

0 Answers0