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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:25:12+00:00 2026-06-13T19:25:12+00:00

I have 3 classes ( A , B , C ) and have to

  • 0

I have 3 classes (A,B,C) and have to implement a storing method for all of the classes so I thought that of using a generic list like List<T> = new List<T>(); but it doesn’t allow me to use it.

I would like the method to be like this:

class Bascket
{
   List<T> list= new List<T>();

   public void addToBasket(T value)
   {
      list.Add(value);
   }
}
  • 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-13T19:25:14+00:00Added an answer on June 13, 2026 at 7:25 pm

    Assuming that A, B and C are items which you wish to store in the Basket object, you should create a base class of those items, and declare generic collection as collection of base class, i.e.

    public interface IBasketItem
    { 
        /* put some common properties and methods here */
        public decimal Price { get; set; }
        public string Name { get; set; }
    }
    public class A : IBasketItem
    { /* A fields */ }
    public class B : IBasketItem
    { /* B fields */ }
    public class C : IBasketItem
    { /* C fields */ }
    
    public class Basket
    {
        private List<IBasketItem> _items = new List<IBasketItem>();
    
        public void Add(IBasketItem item)
        {
            _items.Add(item);
        }
    
        public IBasketItem Get(string name)
        {
            // find and return an item
        }
    }
    

    You can then use Basket class to store all your items.

    Basket basket = new Basket();
    A item1 = new A();
    B item2 = new B();
    C item3 = new C();
    basket.Add(item1);
    basket.Add(item2);
    basket.Add(item3);
    

    However, when retrieving items back, you should use common interface, or you should know of which type the object actually is. E.g:

    IBasketItem myItem = basket.Get("cheese");
    Console.WriteLine(myItem.Name);
    // Take care, if you can't be 100% sure of which type returned item will be
    // don't cast. If you cast to a wrong type, your application will crash.
    A myOtherItem = (A)basket.Get("milk");
    Console.WriteLine(myOtherItem.ExpiryDate);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thought about making enums for all my fields, but that doesn't look
If I have two classes that both implement an interface, but also inherit, do
I have a data access library that has a few classes that all implement
I have a class that instantiates two classes which implement interfaces. I want one
I have an interface that I want to implement in separate classes after doing
I have an interface for a variety of classes, all of which should implement
I am trying to implement my persitent classes using interfaces. I have created the
I have an application that uses several different Java classes and would like to
I have a set of classes ( MyClass1, MyClass2, MyClass3 ) that implement an
I have many classes in a project that need to all have a base

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.