C# Interface<T> { T Func<T>(T t);} : Generic Interfaces with Parameterized Methods with Generic Return Types

You've overspecified the interface. You declare T in the interface definition, but then you redeclare it in the method's definition:

public interface IReadable <T>  /* T is declared here */
{
    T Read<T>(string ID); /* here, you've declare a NEW generic type parameter */
                          /* that makes this T not the same as the T in IReadable */
}

Due to this confusion, you end up with an error when you try to implement the interface.

public class NoteAdapter : IReadable<Note> /* IReadable defines T to be Note */
{
    public Note Read<Note>(string ID) { /* Here, you're declaring a generic parameter */
                                        /* named Note.  This name then conflicts with */
                                        /* the existing type name Note */
        return new Note();
    }
}

To fix this, you simply need to remove the generic parameter from the Read function, both in the interface, and in the NoteAdapter class:

public interface IReadable <T>
{
    T Read(string ID);
}
public class NoteAdapter : IReadable<Note>
{
    public Note Read(string ID) {
        return new Note();
    }
}

EDIT:

Okay, I read the rest of your post, and it seems that you've already discovered that this "works", but you seem to think it's incorrect. Why? What requirements does this not meet?


public interface IReadable <T> { T Read<T>(string ID); }

here really two different Ts : IReadable <T> and T Read<T>(string ID)

maybe just

public interface IReadable <T> { T Read(string ID); } ?

because otherwise is equal to

public interface IReadable <T> { X Read<X>(string ID); }

Good Generics tutorial

EDIT:

May be you need public interface IReadable <T> { T Read(T ID); } ?


The CIL does not support method overloading by return type.

Tags:

C#

Generics