I am trying to understand the concept of .NET Generics and actually use them in my own code, but I have a problem all the time.
Can someone try to explain to me why the following setting does not compile?
public class ClassA
{
ClassB b = new ClassB();
public void MethodA<T>(IRepo<T> repo) where T : ITypeEntity
{
b.MethodB(repo);
}
}
public class ClassB
{
IRepo<ITypeEntity> repo;
public void MethodB(IRepo<ITypeEntity> repo)
{
this.repo = repo;
}
}
I get the following error:
cannot convert from IRepo <T> to IRepo <ITypeEntity>
MethodA is called with the object parameter IRepo <'DetailType>, where DetailType is inherited from ITypeEntity.
I keep thinking that this should compile, as I am restricting T in MethodA to ITypeEntity.
Any thoughts or feedback will be extremely helpful.
Thanks.
Edit: Nick R has a great suggestion, but unfortunately, in my context, I have no way to make ClassA Generic. ClassB maybe.