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);
}
}
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.
You can then use Basket class to store all your items.
However, when retrieving items back, you should use common interface, or you should know of which type the object actually is. E.g: