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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:24:01+00:00 2026-05-14T15:24:01+00:00

Suppose I have a class A, and A1, A2 inherits from A. There are

  • 0

Suppose I have a class A, and A1, A2 inherits from A.
There are 2 functions:

List<A1> getListA1(){...}
List<A2> getListA2(){...}

Now I want to do something similar to both A1 and A2 in another function

public void process(List<A>){...}

If I want to pass the instance of either ListA1 or ListA2, of course the types doesn’t match because the compiler doesn’t allow the coercion from List< A1> to List< A>. I can’t do something like this:

List<A1> listA1 = getListA1();
List<A> newList = (List<A>)listA1; //this is not allowed.

So what is the best approach to the process()? Is there any way to do it in a universal way rather than write the similar code to both List and List?

  • 1 1 Answer
  • 3 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-14T15:24:02+00:00Added an answer on May 14, 2026 at 3:24 pm

    While I can’t offer a java solution, here are some for C# …

    If you can alter the signature of Process to accept IEnumerable …

    public void Process(IEnumerable<A> myList) { ... }
    

    then, in C# 4.0, everything will just work, thanks to the improved support for co- and contra-variance.

    If you’re working in C# 3.0, you can introduce a generic type parameter to the method:

    public void Process<T>(List<T> myList) where T : A { ... }
    

    Then, you can call passing either List or List and the generic type parameter will bind accordingly. Note that you don’t often have to specify it directly, as type inferrence will usually give you what you need.

    If this doesn’t suit, you could convert the List using the Cast extension method from Enumerable:

    public void Process(List<A> myList) { ... }
    
    var someList = getListA1();
    Process( someList.Cast<A>());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a Base class and a Child class that inherits from Base
Suppose I have abstract class A and abstract class B which inherits from A,
Suppose I have a class Dog that inherits from a class Animal . What
Suppose we have 2 classes, Child, and the class from which it inherits, Parent.
Let's suppose I have a class Dog that inherits from class Animal, you might
Suppose now I have three source files: ClassA.hpp, ClassB.hpp, and ClassC.hpp. ClassB inherits from
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
Suppose I have a Base class: class Base { friend SomeOtherClass; }; And there
Suppose I have a class A that does not inherit from anything, has no
Suppose we have a class A and class B and C inherit from it.

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.