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 6816203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:53:51+00:00 2026-05-26T20:53:51+00:00

I have a good understanding of OOP in general, inheritance and polymorphism, interfaces, etc.

  • 0

I have a good understanding of OOP in general, inheritance and polymorphism, interfaces, etc. I encountered a strange situation and I don’t understand why it does not work at all…

EDIT : Ok, I found out that covariance (or contravariance?) may solve this problem, but crucially

we’re still using .NET 2.0

How can I solve this without moving to C# 4.0 ?

Here is the situation. Given these two classes :

public class CustomCollectionType<T> : IEnumerable<T>
{
    /* Implementation here, not really important */
}

public class Entity : EntityBase
{
    /* Implentation here, not important */
}

The compiler complains when I try to have this generic method

public void LoopThrough(IEnumerable<EntityBase> entityList)
{
    foreach(EntityBase entity in entityList) 
    {
        DoSomething(entity);  
    }
}

And try to use it this way :

CustomCollectionType<Entity> entityList;
/* Add items to list */

LoopThrough(entityList);

Error says I cannot convert from CustomCollectionType<Entity> to IEnumerable<EntityBase>.

However, I can do this :

public void Foo(EntityBase entity)
{
    entity.DoSomething();
}

Foo(new Entity());

And this :

public void Bar(IEnumerable<Entity> entityList)
{ ... }

CustomCollectionType<Entity> entityList;

Bar(entityList);

Why can’t I create my method with the highest classes in the hierarchy? The types are obviously compatible… Am I missing something ?

EDIT : I want to solve this problem without altering the existing classes in any way, so creating a new method in any of the classes, or implementing an additional interface is out of the question.

  • 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-26T20:53:51+00:00Added an answer on May 26, 2026 at 8:53 pm

    Let’s consider your first case. You have:

    class Bowl<T> : IEnumerable<T> {}
    class Apple : Fruit {}
    ...
    void LoopThrough(IEnumerable<Fruit> fruits) ...
    

    and you call

    Bowl<Apple> apples = whatever;
    LoopThrough(apples);
    

    This fails in C# 3.0; it succeeds in C# 4.0 because IEnumerable<T> is now covariant in T; a sequence of apples can be used as a sequence of fruits.

    To make it work in C# 3.0 you can use the Cast sequence operator.

    Bowl<Apple> apples = whatever;
    LoopThrough(apples.Cast<Fruit>());
    

    To make it work in C# 2.0, implement the Cast sequence operator yourself. It is only a couple lines of code.

    Note that in C# 4.0 it will still not be legal to say:

    Bowl<Fruit> fruits = new Bowl<Apples>();
    

    because of course you can say:

    fruits.Add(new Orange());
    

    and you just put an orange into a bowl that can only contain apples.

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

Sidebar

Related Questions

I don't have a very good understanding of Javascript so appologies before we start.
I don't have a very good understanding of Javascript so appologies before we start.
I thought that i have some good understanding of Java generics. This code DOES
I have a pretty good understanding of Javascript, except that I can't figure out
I am primary a web developer but I do have a very good understanding
There are many skills a programmer could have (understanding the problem, asking good questions,
Does gwt have (good) support for effects, moving around elements, and resizing them? Are
I’m currently learning the Java Collections API and feel I have a good understanding
I have a fairly good understanding of the dereferencing operator, the address of operator,
I have a good understanding of C, C++ and java. I have an idea

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.