Container<ImplementerOfIInterface> is not Container<IInterface>. Why not?

Posted by Chris Simmons on Stack Overflow See other posts from Stack Overflow or by Chris Simmons
Published on 2010-06-12T14:05:56Z Indexed on 2010/06/12 14:12 UTC
Read the original article Hit count: 177

Filed under:
|
|

Why wouldn't DoesntWork() work below? The error is: Cannot implicitly convert type 'List' to 'IEnumerable'. An explicit conversion exists (are you missing a cast?). I know this is something about generic/templates I'm not getting, but List is IEnumerable and Implementer is an IInterface. I don't see why this needs to be casted (or if it really can be).

public interface IInterface
{
    // ...
}

public class Implementer : IInterface
{
    // ...
}

IEnumerable<IInterface> DoesntWork()
{
    List<Implementer> result = new List<Implementer>();
    return result;
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about inheritance