TheBoubou
TheBoubou

Reputation: 19903

Return a generic, implicit reference

public class Display<T> where T : class, IDisplay<T>
{
    public List<T> MyList { get; set; }
    public int Total { get; set; }

    public Display(List<T> myList, int total)
    {
        MyList = myList;
        Total = total;
    }
}

public interface IDisplay<T> where T : class
{
    List<T> MyList { get; set; }
    int Total { get; set; }
}


MyClass() : IMyClass
{
}

public interface IMyClass
{
}

When I use :

return new Display<IMyClass>(listOffIMyClass, anIntValue); 

I get this error : IMyClass cannot be used as type parameter 'T' in the generic type or method 'Display'. There is no implicit reference conversion from 'IMyClass' to 'IMyClass'.

Upvotes: 2

Views: 123

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500185

Well, yes - you've said that the T used for a Display<T> has to implement IDisplay<T>, and IMyClass doesn't implement IDisplay<IMyClass>.

Did you actually just mean to make Display<T> implement IDisplay<T>? If so, you want:

public class Display<T> : IDisplay<T> where T : class

Now you're still constraining T to be a reference type, but you're not constraining T to implement IDisplay<T>.

Upvotes: 7

Related Questions