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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:54:30+00:00 2026-05-12T14:54:30+00:00

I am using a hierarchy of generic collection classes that derive from an abstract

  • 0

I am using a hierarchy of generic collection classes that derive from an abstract base class to store entity items that also derive from an abstract base class:

abstract class ItemBase { }

class MyItem : ItemBase
{
    public MyItem()
    {
    }
}


abstract class CollectionBase<T> : Collection<T> where T : ItemBase, new() { }

class MyCollection : CollectionBase<MyItem> { }

The objectives of this model are to enforce strict type discipline on the members of any class derived from CollectionBase<T> and to guarantee that these members have a default public constructor. Thus far it works.

Now I want to create a factory method that returns an instance of a class derived from CollectionBase<T>. I understand that the usual approach would be:

public CollectionBase<T> CreateCollection<T>();

… or maybe …

public T Create CreateCollection<T>();

However, the problem is that the calling routine does not know what “T” is required. It is the factory method itself that must determine the specific type of collection to return. So I need a non-generic method signature that says “the return type will derive from CollectionBase<T>”. I envisage something like this (if it were legal) …

public CollectionBase<> CreateCollection();

I assume this is another of those thorny generic variance issues, but even after reading Eric Lippert’s extensive explanation on the subject, I am still unclear whether what I am trying to do is likely to be feasible in C# 4.0, and whether there is a simple workaround in C# 3.0.

Thanks in advance for your ideas.

  • 1 1 Answer
  • 1 View
  • 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-12T14:54:30+00:00Added an answer on May 12, 2026 at 2:54 pm

    Let’s say the factory method were creating a single item. You would have:

    public ItemBase CreateItem();
    

    You have to use ItemBase because you can’t know anything more specific. Applying that principle to the collection method, you get:

    public CollectionBase<ItemBase> CreateCollection();
    

    Due to lack of variance, you need an adapter class which derives from CollectionBase<ItemBase> and wraps the actual collection which is created.

    In C# 4.0 you would just return the actual collection.

    Edit: The factory might look like this, with the adapter embedded:

    public class CollectionFactory
    {
        public CollectionBase<ItemBase> CreateCollection()
        {
            if(someCondition)
            {
                return CreateAdapter(new MyCollection());
            }
            else
            {
                return CreateAdapter(new MyOtherCollection());
            }
        }
    
        private static CollectionAdapter<T> CreateAdapter<T>(CollectionBase<T> collection) where T : ItemBase, new()
        {
            return new CollectionAdapter<T>(collection);
        }
    
        private class CollectionAdapter<T> : CollectionBase<ItemBase> where T : ItemBase, new()
        {
            private CollectionBase<T> _collection;
    
            internal CollectionAdapter(CollectionBase<T> collection)
            {
                _collection = collection;
            }
    
            // Implement CollectionBase API by passing through to _collection
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to map the following class hierarchy using Entity Framework 4.3. public abstract
If have a type hierarchy that starts out with a generic type class A<T>
I have two interfaces, a generic and a non-generic that have an inheritence hierarchy:
I am using a hierarchy of inner classes to represent some data in an
Let's say we have a class hierarchy where we have a generic Animal class,
We have the following class hierarchy: public class Base { public Base() { //
Below is my element hierarchy. How to check (using XPath) that AttachedXml element is
I'm using Entity Framework with a set of 3 different tables, in a hierarchy.
Suppose you have the following Generic class heirarchy: public abstract class GenericBase<T> { T
We have a DAL that we're using with NHibernate.Search , so classes that need

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.