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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:34:45+00:00 2026-05-27T13:34:45+00:00

I have two lists. The types of both lists inherit from the same base

  • 0

I have two lists. The types of both lists inherit from the same base type. I want to iterate through them and do operations which only use functionality of the base class without having two basically identical foreach loops one after the other.

I can’t copy the lists to another list or something of the sort, as I need to use the lists in their original form separately after the operation is complete.

Is there a way to do this without writing a function?

class Program
{
    static void Main(string[] args)
    {
        // I have two lists of inherited classes
        List<Babby1> list1 = returnBabby1();
        List<Babby2> list2 = returnBabby2();

        // I want to iterate through both, and do the same thing, which is a part
        // of the base class functionality.
        // Basically I want this to be a single foreach loop.
        foreach (Babby1 item in list1)
            item.var = 50;

        foreach (Babby2 item in list2)
            item.var = 50;

        // I have to send them as separate lists, the type being the original (inherited) class
        sendBabby1(list1);
        sendBabby2(list2);
    }

    static void sendBabby1(List<Babby1> list)
    {
    }

    static void sendBabby2(List<Babby2> list)
    {
    }

    static List<Babby1> returnBabby1()
    {
        return new List<Babby1>();
    }

    static List<Babby2> returnBabby2()
    {
        return new List<Babby2>();
    }
}

class Base
{
    public int var;
}

class Babby1 : Base
{
    public int var1;
}

class Babby2 : Base
{
    public int var2;
}
  • 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-27T13:34:46+00:00Added an answer on May 27, 2026 at 1:34 pm

    This should do the trick…

    foreach (var item in list1.Concat<Base>(list2))
    {
        // Do your thing
    }
    

    EDIT: I changed Union to Concat as I think that it’s probably more appropriate.

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

Sidebar

Related Questions

I have two generic lists of type T. Both lists contain same type, and
I have two lists which are guaranteed to be the same length. I want
I have two ordered lists of the same element type, each list having at
I have a class Graph with two lists types namely nodes and edges I
I have two lists of custom objects and want to update a field for
I have two lists and I want to select items that are not in
I have two lists of files, one from a local drive and one from
I currently have two types of Lists: List (named LintMessages) and List (named FilterList).
I have two ObservableCollection lists, that i want to unite. My naive approach was
I have two List of two different custom types. Both types share 2 common

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.