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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:47:18+00:00 2026-05-11T20:47:18+00:00

I have several classes (A, B, C, …) that all use a List<AnotherClass> to

  • 0

I have several classes (A, B, C, …) that all use a List<AnotherClass> to store references to ‘other’ objects. But ‘other’ is different for each of the classes A, B, C.
So
Class A contains List<Class_X>
Class B contains List<Class_Y>
Class C contains List<Class_Z>

Instead of implementing Add / Delete / Search (etc) functions in A, B, C it seems logical to me to create a class ListRef<T> from List<T>

public class ListRef<T>: List<T>
{
    protected ListRef<T> ListOfObjects = null;
    protected string name = null;

    public ListRef<T>
    {
        ListOfObjects = new ListRef<T>();
    }
}

Using the code above (is this the right code for what I want?) I don’t know how I can supply the right class (Class_X, Class_Y, Class_Z) replacing/specifying <T> in the constructor of each class (A, B, C) that will use ListRef.

In the constructor of class A I would like to write something like:

public A() : base<Class_X>
{
}

How can I specify from WITHIN class A what kind of objects need to be stored in ListOfObjects?

I prefer NOT to write

public A()
{
    ListOfObjects = new ListRef<Class_X();
}

as I would like to have ListOfObjects declared private instead of protected


Inside Listref I JUST want to be able to Add, Delete, Search objects. So I’m not actually using those classes (Class_X, Class_Y, Class_Z).

currently I have

public class A
{
     private List<Class_X> ListOfObjects = null;
     A()
     {
         ListOfObjects = new List<Class_X>();
     }

     public void Add(string Name)
     {
        Class_X Object = new Class_X(Name);
        ListOfObjects.Add(Object);
     }
     public void Delete(Class_X Object)
     {
        ListOfObjects.Remove(Object);
     }
}

and the same kind of code for class B (using Class_Y) and for class C (using class_Z).
To me it seems logical to use ONE class ListRef to perform the Add and Delete operations and maintain the list for all classes I use.
(of course the real code is more complicated)

  • 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-11T20:47:18+00:00Added an answer on May 11, 2026 at 8:47 pm

    If I understand you question correctly, it sounds like what you want to do is create a group of classes A, B, C, etc.. that each manage a collection of some other type (X, Y, Z) – but you don’t want to duplicate some of the list management logic across A, B, and C.

    There are two different ways to achieve this.

    First, the inheritance approach: you could give A, B, and C a common generic base class that is parameterized on the type of the item each will manage. Here’s a code example:

    public abstract class ABCBase<T>
    {
       protected IList<T> m_List = new List<T>();
    
       // methods that manage the collection
       // I chose to make the virtual so that derived 
       // classes could alter then behavior - may not be needed
       public virtual void Add( T item )    { ... }
       public virtual void Remove( T item ) { ... }
       public virtual int  Find( T item )   { ... } 
    }
    
    public class A : ABCBase<X> { ... }
    
    public class B : ABCBase<Y> { ... }
    
    public class C : ABCBase<Z> { ... }
    

    Second, is the composition approach: create a manager class for your colleciton that implements the operations on the child list, and aggregate that in each of A, B, and C:

    public class ListManager<T>
    {
       private IList<T> m_List = new List<T>();
    
       public void Add( T item )    { ... }
       public void Remove( T item ) { ... }
       public int  Find( T item )   { ... }
    }
    
    public class A
    {
       public ListManager<X> ListOfX { get; protected set; }
    
       public A() { ListOfX = new ListManager<X>(); }
    }
    
    public class B
    {
       public ListManager<Y> ListOfX { get; protected set; }
    
       public B() { ListOfY = new ListManager<Y>(); }
    }
    
    public class C
    {
       public ListManager<Z> ListOfX { get; protected set; }
    
       public C() { ListOfX = new ListManager<Z>(); }
    }
    

    You could also choose to mix both of these approaches – creating a list management class but also creating base class (or interface) for A, B, C – so that each exposes a consistent property ChildList (or some such) that consumers could use without always having to know the type actual types A, B, C.

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

Sidebar

Related Questions

I have several classes, that all derives from SuperClass. When the classes are created,
i have several classes that have the same function, the are called at different
If I have several classes in different php files that I'm including into a
I have several classes that use the same JNI library, called jni. In each
I have several classes which use static variables. I assume that the classes are
Sometimes we have several classes that have some methods with the same signature, but
i have several classes with members called 'Id'. Originally i wanted to store these
This is for a web project so i have several classes that inherit from
I have several small classes that are in a single file in /app/models, similar
I have several classes that are basically interfaces to database rows. Since the class

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.