Basically I have a custom List class that contains different fruits. Assume that each fruit has an ID number that is stored in the list.
Is it better to have:
new AppleList();
new OrangeList();
new LemonList();
or
new FruitList<Fruit.Apple>();
new FruitList<Fruit.Orange>();
new FruitList<Fruit.Lemon>();
Things to consider:
I would like to use the one that is clearer, better in design, faster, more efficient, etc. Additionally if these above 2 techniques are not the best, please suggest your ideas.
EDIT: Btw Fruit is an enum if that wasn't clear.
Cost is the main difference between generic and brand name prescription drugs. Unlike brand companies, generic manufacturers compete directly on price, resulting in lower prices for consumers. Generics have saved Americans $2.2 trillion over the last decade.
Use generic types to maximize code reuse, type safety, and performance. The most common use of generics is to create collection classes. The . NET class library contains several generic collection classes in the System.
The Cons of Generic DrugsMedicines can look different: Trademark laws prohibit a generic drug from looking exactly like its brand-name version, so if you've switched to a generic drug, its shape, color or size may be different from what you're accustomed to taking.
Code Reuse: With help of Generics, one needs to write a method/class/interface only once and use it for any type whereas, in non-generics, the code needs to be written again and again whenever needed.
Use a combo:
public class AppleList : FruitList<Apple> { ... }
public class OrangeList : FruitList<Orange> { ... }
public class LemonList : FruitList<Lemon> { ... }
Put the common logic in the base list class:
public class FruitList<T> : List<T>
where T : IFruit
{ ... }
If you use generics, is there a purpose to create the FruitList type? Could you just use List?
There won't be much difference in performance, so I say why create three different classes when one would do the same exactly thing? Use the generic solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With