C# Interfaces – Why Can’t Generic Methods Implement Multiple Typed Interfaces?

cinterfaces

public class B { }
public class C { }
public class D { }
public class E { }

public class A :
    IRetrievable<B, C>,
    IRetrievable<D, E>
{
    public TValue Retrieve<TKey, TValue>(TKey input)
    {
        throw new NotImplementedException();
    }
}

What is the reason I can't do this? Visual Studio is telling me that the interfaces aren't being implemented, even though they could be via putting types B, C and D, E in TKey, TValue, respectively.

Best Answer

The generic type will have the actual types used in the code added at compile time.

Here you are confusing Method and class generic types

public class B { }
public class C { }
public class D { }
public class E { }

public class A :
    IRetrievable<B, C> //interface not implemented!!
{
    public TValue Retrieve<TKey, TValue>(TKey input)
    {
        throw new NotImplementedException();
    }
}

public interface IRetrievable<T1, T2>
{
    T1 Retrieve(T2 input);
}

public void Main()
{
    var a = new A()
    a.Retrieve<D,E>(new D());
}

Here A should implement IRetrievable for A and B. but the method is called with D and E and so a class A_DandE will be created which doesn't match the interface

A must implement the interface as specified, but the actual implementation is defined by the calling code and as such cant be guaranteed when you just compile A on its own

working code:

public class A :
    IRetrievable<B, C>, 
    IRetrievable<D, E>
{
    public B Retrieve(C input)
    {
        throw new NotImplementedException();
    }

    public D Retrieve(E input)
    {
        throw new NotImplementedException();
    }
}

Here A implements both methods as defined by the interfaces