A Solution For (IEnumerable<Base>)Derive; Yet?

Posted by acidzombie24 on Stack Overflow See other posts from Stack Overflow or by acidzombie24
Published on 2010-05-29T02:36:41Z Indexed on 2010/05/29 2:42 UTC
Read the original article Hit count: 132

Filed under:
|
|
|

I have D1 and D2 which derive from B. When i write var ls = (IEnumerable<B>)(cond?lsD1:lsD2); I get a runtime cast error. IIRC this is a well known problem. My question is

1) Is this allowed yet? perhaps in .NET 4? I have 2010 but my project is a few months old, large and targets 3.5.

2) Is there a simple workaround? I only need to read the list not add anything or remove. Actually, ToArray() would probably work but is there another solution?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET