1
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.

  • Well, you've specified two interfaces, but only implemented one (which one?). Even if you implemented the other, how would C# tell the difference between the two, since they are both exactly the same signature? – Robert Harvey Jan 3 at 15:28
  • I was hoping the compiler would be clever and know that it can use the same implementation for both, since both fit into the same method. – Tim Morris Jan 3 at 15:30
  • actually i don't think this works with even a single interface – Ewan Jan 3 at 15:46
  • @Ewan It doesn't, and I don't know why. – Tim Morris Jan 3 at 15:52
2

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

|improve this answer|||||
  • I guess! In my particular use case it would save so much time if I could do this. Oh well. – Tim Morris Jan 3 at 15:54
  • @TimMorris this could be a job for T4. For example, you can keep your generic implemention on your class, then generate a class part with T4 which implements the interfaces delegating to it. – Theraot Jan 3 at 16:21
1

Use composition instead. Pass implementations of your two interfaces as parameters into the constructor of your class, and assign each one to an IRetrievable<T, K> member of your class.

Alternatively, inherit from a dual interface:

public interface IDualRetrievable
{
    IRetrievable<T, K> Retrievable1 { get; set; }
    IRetrievable<T, K> Retrievable2 { get; set; }
}

public class MyClass : IDualRetrievable

If you wish, you can pass the same implementation for each interface instance.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.