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

  • Home
  • SEARCH
  • 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 6668475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:02:45+00:00 2026-05-26T03:02:45+00:00

I have setup my classes as below. So both collections implement a common interface

  • 0

I have setup my classes as below.
So both collections implement a common interface – IChild.

Is there a way for a method on the parent to return the common parts of each collection please?

I am using “+” below, and I know this doesn’t work, but it indicates what I am trying to do.

public interface IChild
{
    DateTime Date { get; }
    Parent Parent { get; set; }
}

public class Boy : IChild
{
    public virtual Parent Parent { get; set; }
    public virtual DateTime GraduationDate { get; set; }
    public virtual DateTime Date { get { return ProcedureDate; } }
}

public class Girl : IChild
{
    public virtual Parent Parent { get; set; }
    public virtual DateTime WeddingDate { get; set; }
    public virtual DateTime Date { get { return ContactDate; } }
}


public Parent
{
    protected IList<Boy> _boys = new List<Boy>();
    protected IList<Girl> _girls = new List<Girl>();

    public virtual IEnumerable<Boy> Boys
    {
        get { return _boys; }
    }

    public virtual IEnumerable<Girl> Girls
    {
        get { return _girls; }
    }

    public virtual IEnumerable<IChild> Children
    {
        get
        {
            return _boys + _girls;
        }
    }
}

Thanks

  • 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-26T03:02:45+00:00Added an answer on May 26, 2026 at 3:02 am

    You can use Cast<T> to cast to the interface and then Concat to stream one sequence after the other.

    return _boys.Cast<IChild>().Concat(_girls.Cast<IChild>()); // .NET 3.5 and greater
    

    Note: If you are using .NET 3.5 you would need to perform the cast as shown here. If you are on .NET 4, you can follow Kristian Fenn’s answer and omit the cast in lieu of .Concat<IChild>, which works because they made IEnumerable<T> covariant in the .NET 4 release, which allows IEnumerable<Boy> to be substituted when methods call for IEnumerable<IChild>.

    return _boys.Concat<IChild>(_girls); // .NET 4.0 and greater
    

    For completion, if you still happen to be using .NET 2.0, neither approach applies, although it’s trivial to implement yourself.

    public virtual IEnumerable<IChild> Children // .NET 2.0 and greater
    {
        get
        {
            foreach (IChild boy in _boys)
                yield return boy;
            foreach (IChild girl in _girls)
                yield return girl;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have setup a property and implement INotifyPropertyChanged like so... public event PropertyChangedEventHandler PropertyChanged;
I have a menu setup with the below css: a.menu:active { color: #000000; }
I have a build setup that requires that my classes be put into a
I have a simple 'Service' system set up with an interface as shown below.
i have problem with autorotate on iphone i set up in all classes -
I have a NetBeans project set up with a bunch of source classes and
I have a field for addedBy in one of my classes. I've set up
I have setup web dav on windows server 2008. It seems to work fine
I have setup svnserve and for now now I am testing a repository with
I have setup multiple SQL Service Broker Queues in a database but have not

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.