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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:34:55+00:00 2026-05-12T19:34:55+00:00

I feel my question is pretty dumb, or another way to put it is

  • 0

I feel my question is pretty dumb, or another way to put it is : I’m too lost in my code to see a workaround for now. Stay too long on a problem, and your vision becomes narrower and narrower ><. Plus I’m not good enough with inheritance, polymorphism and so

Here is the idea : I have multiple list of derived class, and I would like to call generic functions on those lists (accesing and modifying members of the base class). I feel there is something to do with inheritance, but I don’t manage to make it work as I want for now .

Here is a very simple example of what I’m intending to do :

class Baseclass
{
    public int ID;
    public string Name;
}
class DerivedClass1 : Baseclass
{
}

private void FuncOnBase(List<Baseclass> _collection)
{
    // ...

    foreach (Baseclass obj in _collection)
    {
        ++obj.ID;
    }

    // ...
}
private void FuncTest()
{
    List<DerivedClass1> collection1 = new List<DerivedClass1>();
    collection1.Add(new DerivedClass1() { ID = 1 });
    collection1.Add(new DerivedClass1() { ID = 2 });
    collection1.Add(new DerivedClass1() { ID = 3 });

    FuncOnBase(collection1);   //  ==> forbidden, cannot convert the derived class list to the base class list
}
  • 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-12T19:34:55+00:00Added an answer on May 12, 2026 at 7:34 pm

    Gotta love variance. A List<DerivedClass1> is not a List<Baseclass> – otherwise, FuncOnBase could attempt to add a Baseclass to the list, and the compiler wouldn’t spot it.

    One trick is to use a generic method:

    private void FuncOnBase<T>(List<T> _collection) where T : Baseclass
    {
        // ...
    
        foreach (T obj in _collection)
        {
            obj.ID++;
        }
    
        // ...
    }
    

    In terms of the example I presented above – note that we are able to add a T to the list; useful in particular if we add the T : new() constraint, or pass in (for example) a params T[].

    Note also that IEnumerable<T> becomes covariant in C# 4.0 / .NET 4.0, so if you passed in just an IEnumerable<Baseclass> (rather than a list) it would work “as is”:

    private void FuncOnBase(IEnumerable<Baseclass> _collection)
    {
       ///...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK - I feel pretty dumb asking such a basic question, but hey. I'm
I feel like this is probably a pretty dumb question, but I am just
Basic question so I feel dumb but..., Whats the proper syntax below in the
Yes this is a pretty general question but I'm trying to get a feel
I feel like I am asking a pretty basic question, and I think I
So this is a pretty dumb question, and one I am clearly misunderstanding for
I'm pretty sure that this is the right site for this question, but feel
I feel like this is probably a pretty simple question, but this is my
Update & Summary I feel obligated to make this question clearer, now that there
I have a python question that I'm sure is pretty simple - please feel

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.