Here is an example :
public class B<T> {}
public class D : B<int> {}
public class A<T, S> where T : B<S> {}
public class C : A<D, int> {}
public class Test1
{
public class test1
{
A<D, int> t = new C();
}
}
What I would like do to is in declaring class C, only say : C : A<D>. Why I need to repeat int ? Because int is already a part of D.
Same in the test1 method. I would like to write : A<D> t = new C();
How, can I achieve that ?
UPDATE
Here with more realistic class names :
public class MyModel<T> { }
public class MyTrueModel : MyModel<int> { }
public class MyManager<T,S> where T : MyModel<S> { }
public class MyTrueManager : MyManager<MyTrueModel, int> { }
public class Test1
{
public class test1
{
MyManager<MyTrueModel, int> t = new MyManager<MyTrueModel, int>();
}
}
All the problem come from the class MyManager. If I was able to do something like : MyManager<T> where T : MyModel it’d would be great.
Here’s your code:
Here’s equivalent code:
The point is that the
UinA<U, V>is a dummy. When you replaceUwithT(andVwithS) and writeA<T, S>theTdoes not refer to the sameTinB<T>. This is why you must useC : A<D, int>. If you were to only writeA<D>the compiler does not know (and nor should it; see my comment below on free versus unbound variables) that you want to useintforTinB<T>.This is not possible.
MyModelis not declared as a type. OnlyMyModel<T>is a type. More specifically, it is an unbounded generic type. When you specify a type argument (e.g.,MyModel<int>) then it will be a constructed type.At this risk of confusing you further (on this admittedly confusing issue), it might help you read about free and unbounded variables.