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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:32:57+00:00 2026-06-14T03:32:57+00:00

Say I have several derived classes whose base class is a generic class. Each

  • 0

Say I have several derived classes whose base class is a generic class. Each derived class inherit the base class with a specific type override(but all types are also derived from a single base type).

For example:

I have a base row class

class RowBase
{
    //some properties and abstract methods
}

And I have two specific row classes that are derived from the row base class

class SpecificRow1 : RowBase
{
    //some extra properties and overrides
}

class SpecificRow2 : RowBase
{
    //some extra properties and overrides
}

Then I have a second base class that is a generic class which contains a collection of derived classes from RowBase

class SomeBase<T> where T : RowBase
{
    ICollection<T> Collection { get; set; }
    //some other properties and abstract methods
}

Then I have two classes that derive from SomeBase but are using different specific row class

class SomeClass1 : SomeBase<SpecificRow1>
{
     //some properties and overrides
}

class SomeClass2 : SomeBase<SpecificRow2>
{
     //some properties and overrides
}

Now that in my main or a bigger scope, I want to create a list/collection that consist both SomeClass1 and SomeClass2 objects. Like

ICollection<???> CombinedCollection = new ...
CombinedCollection.Add(new SomeClass1())
CombinedCollection.Add(new SomeClass2())
.
.
.
//add more objects and do something about the collection
.
.
.

The question is: is it possible to have such collection? If it is possible, how can I achieve this? If no, what can be an alternative way?

  • 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-14T03:32:58+00:00Added an answer on June 14, 2026 at 3:32 am

    This can be done with the help of Covariance and Contravariance.

    Add a new interface that and make the T parameter covariant (using the out keyword):

    interface ISomeRow<out T> where T : RowBase
    {
    }
    

    SomeBase should inherit that interface like this:

    class SomeBase<T> : ISomeRow<T> where T : RowBase
    {
        //some other properties and abstract methods
    }
    

    Then, the following will work:

    List<ISomeRow<RowBase>> myList = new List<ISomeRow<RowBase>>();
    myList.Add(new SomeClass1());
    myList.Add(new SomeClass2());
    

    Hope this is what you’re looking for 🙂

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

Sidebar

Related Questions

Let's say I have a Base class and several Derived classes. Is there any
Say I have several objects within a class, each of which needs constructing with
I have a base class, Primitive , from which I derive several other classes--
Say I have several ObservableCollections of different classes: public class Employee { public int
I have a base class and several concrete classes that derive from it. Lets
Let's say I have several lines of code to clarify specific settings on a
Let's say we have several exception classes that extends exception. My question is, in
Let's say I have a class designed to be instantiated. I have several private
Say I have two classes with different names but the exactly same structure. It
Not sure how to implement this scenario: I have a base class let's say

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.