I have created a method with two generic parameters where one parameter (itemsToAdd
) must be the same type as the generic parameter of the next parameter (inputList
).
See this demo code:
public class GenericsDemo
{
public void AddToList<TList, TItems>(TList inputList, params TItems[] itemsToAdd)
where TItems : IConvertible
where TList : IEnumerable<TItems>
{
IEnumerable<IConvertible> someOtherList;
// Sounds good, doesn't work..
//someOtherList = inputList;
// This works
someOtherList = (IEnumerable<IConvertible>)inputList;
}
}
I would expect the inputList
can be directly assigned into the IEnumerable<IConvertible> someOtherList
, but it needs a cast. Why the cast is needed?