I have a generic interface IConstrained which is implemented by the generic Constrained class. When I attempt to do the code below I get an invalid cast exception.
IConstrained<decimal> decimalLimit = new Constrained<decimal>(1);
IConstrained<IComparable> comparableLimit = (IConstrained<IComparable>) decimalLimit;
Why is it not possible to do this if decimal implements IComparable? What would be the correct way to do this? Thanks.
Generic types are not covariant in .NET 2.0. This includes .NET 3.0/3.5 as well since they use the same 2.0 runtime. .NET 4.0 will support covariance, however.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With