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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:58:42+00:00 2026-06-13T19:58:42+00:00

Coming from Java or Python I am used to making such constructs and normally

  • 0

Coming from Java or Python I am used to making such constructs and normally it “just works”. Been discovering C# lately and, other than a few frustrations, find it quite pleasant to work with.

One such frustration is how much I have to hold it’s hands through polymorphism constructs (override, virtual, etc).

Here is one such case I have not yet found how to “just make work”.

class Polymorphism
{
    public interface IPerson
    {
        String Name { get; set; }
    }

    public class Doctor : IPerson
    {
        public String Name { get; set; }
    }

    public class DoctorGroup
    {
        public IEnumerable<IPerson> Members
        {
            get
            {
                return DoctorMembers;
            }
        }

        public List<Doctor> DoctorMembers { get; set; }
        public DoctorGroup()
        {
            DoctorMembers = new List<Doctor>();
        }
    }
}

Here the DoctorMembers are IPersons, thus I should be able to just return a list of doctors when asked for an IEnumerable of IPerson.

public IEnumerable<IPerson> Members
{
    get
    {
        return DoctorMembers;
    }
}

Yet… the compiler complains… why? What am I missing? Semantically there is nothing wrong with this, most OOP languages I have worked with thus far can digest this without syntactic Pepto-Bismol. Am I missing a keyword to make it obvious to the compiler?

The obvious shortcut here is to LINQ convert all the doctors to IPerson but I fail to see why this is necessary or even desirable.

Anybody can light my lantern?

  • 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-06-13T19:58:43+00:00Added an answer on June 13, 2026 at 7:58 pm

    This code does compile with C# 4 targeting .NET 4, but wouldn’t have compiled in C# 3, which didn’t have generic variance.

    Basically you want an implicit conversion from IEnumerable<Doctor> to IEnumerable<IPerson>. That’s safe, because you can only get values “out” of an IEnumerable<T>… which is why in .NET 4, it’s declared as IEnumerable<out T>.

    Read more on generic variance for details, and remember that this is only available in C# 4 and upwards.

    (If you have to use C# 3 or .NET 3.5, you can use the solution shown by CSharpie… but ideally, upgrade to a more recent version 🙂

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

Sidebar

Related Questions

I just started learning C++(coming from Java & Python) and am trying to learn
Coming to Python from Java, I've been told that factories are not Pythonic. Thus,
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering
Coming from Java , I'm used to the package structure (com.domain.appname.tier) Now I've started
I just started learning C++ (coming from Java ) and am having some serious
I'm coming from a place without macros (Java/Python/C#/Scala) so perhaps my perspective is distorted
I'm new to Python... and coming from a mostly Java background, if that accounts
Coming from Java and Python, I am not so well versed in memory management,
Coming from a java background I'm used to having a bunch of properties files
I haven't done much python - coming from a C/Java background - so excuse

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.