I have the following inheritance:
internal abstract class TeraRow{}
internal class xRow : TeraRow {} // xRow is a child of TeraRow
public IEnumerable<TeraRow> Compare(MappedTables which, DateTime selectionStart
, DateTime selectionEnd, string pwd)
{
IEnumerable<xRow> result=CompareX();
return (IEnumerable<TeraRow>)result; //Invalid Cast Exception?
}
Unable to cast object of type 'System.Collections.Generic.List1[xRow]' to type 'System.Collections.Generic.IEnumerable
1[TeraRow]
Also why do I need to cast it at all?
You need to cast it because IEnumerable<T>
is not covariant on T. You can do this:
return result.Cast<TeraRow>();
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