Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5972363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:38:50+00:00 2026-05-22T20:38:50+00:00

I generally understand interfaces, inheritance and polymorphism, but one thing has me puzzled. In

  • 0

I generally understand interfaces, inheritance and polymorphism, but one thing has me puzzled.

In this example, Cat implements IAnimal and of course List implements IList:

IList<IAnimal> cats = new List<Cat>();

but it generates a compilation error (Cannot implicitly convert type…). It also won’t work if I use an asbtract superclass [Animal] that Cat inherits from. However, If I replace IAnimal with Cat:

IList<Cat> cats = new List<Cat>();

it compiles fine.

In my mind, because Cat implements IAnimal, the first example should be acceptable, allowing us to return an interface for both the list and the contained type.

Can anyone explain why it isn’t valid? I’m sure there’s a logical explanation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:38:50+00:00Added an answer on May 22, 2026 at 8:38 pm

    There is a logical explanation, and this exact question is asked pretty much every day on StackOverflow.

    Suppose this was legal:

    IList<IAnimal> cats = new List<Cat>(); 
    

    What stops this from being legal?

    cats.Add(new Giraffe());
    

    Nothing. “cats” is a list of animals, and a giraffe is an animal, and therefore you can add a giraffe to a list of cats.

    Clearly that is not typesafe.

    In C# 4 we added a feature whereby you can do that if the metadata annotations allow the compiler to prove that it is typesafe. In C# 4 you can do this:

    IEnumerable<IAnimal> cats = new List<Cat>(); 
    

    because IEnumerable<IAnimal> has no Add method, so there is no way to violate type safety.

    See my series of articles on how we designed this feature in C# 4 for more details.

    http://blogs.msdn.com/b/ericlippert/archive/tags/covariance+and+contravariance/default.aspx

    (Start from the bottom.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand how interfaces work in general but I am confused about a thing.
Generally when I use ClickOnce when I build a VB.NET program but it has
As I understand it, the Freebase taxonomy generally boils down to this hierarchy: Domain
I have a good understanding of OOP in general, inheritance and polymorphism, interfaces, etc.
This question is meant to apply to interfaces in general, but I'll use AS3/Flex
I am looking through some example Fibonacci sequence clojure code: (def fibs (lazy-cat [1
I'm new to Flex. Could someone help me understand how Flex generally works with
I'am talking about this example of a Scilab<->C wrapper: http://www.scilab.org/doc/intro/node89.html . The strange part
So, I understand that the following doesn't work, but why doesn't it work? interface
Class A can implement multiple interfaces. What I do not understand is what happens

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.