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’.
Well, yes – you’ve said that the
Tused for aDisplay<T>has to implementIDisplay<T>, andIMyClassdoesn’t implementIDisplay<IMyClass>.Did you actually just mean to make
Display<T>implementIDisplay<T>? If so, you want:Now you’re still constraining
Tto be a reference type, but you’re not constrainingTto implementIDisplay<T>.