16
If I have a generic Item class that looks like this:
abstract class Item<T>
{
}
And a Container of Items that looks like this:
class Container<TItem, T>
where TItem : Item<T>
{
}
Since TItem depends on T, is it possible to simplify the type signature of Container so that it takes only one type parameter? What I really want is something this:
class Container<TItem>
where TItem : Item // this doesn't actually work, because Item takes a type parameter
{
}
So I can instantiate it as follows:
class StringItem : Item<string>
{
}
var good = new Container<StringItem>();
var bad = new Container<StringItem, string>();
The compiler should be able to deduce that T is string when TItem is StringItem, right? How do I make this happen?
Desired usage:
class MyItem : Item<string>
{
}
Container<MyItem> container = GetContainer();
MyItem item = container.GetItem(0);
item.MyMethod();