Generics or not generic

Basically I have my own List class that contains different fruits. Suppose each fruit has an identification number, which is stored in a list.

Better to have:

new AppleList(); new OrangeList(); new LemonList(); 

or

 new FruitList<Fruit.Apple>(); new FruitList<Fruit.Orange>(); new FruitList<Fruit.Lemon>(); 

What to consider:

  • All identifiers are of type int.
  • The type of fetus does not affect the implementation of the List itself. It will be used only by the list client, as an external method, etc.

I would like to use the one that is clearer, better in design, faster, more efficient, etc. Also, if these 2 methods above are not the best, suggest your ideas.

EDIT: Btw Fruit is an enumeration if it's not clear.

+6
generics design c #
source share
8 answers

• All identifiers are of type int.

• The type of fetus does not affect the implementation of the List itself. It will only be used by the list client, such as an external method, etc.

Given these two facts, I would not worry about generics. I would put a regular property in FruitList to indicate what type of fruit it has.

+3
source share

Use combos:

 public class AppleList : FruitList<Apple> { ... } public class OrangeList : FruitList<Orange> { ... } public class LemonList : FruitList<Lemon> { ... } 

Put the general logic in the base list class:

 public class FruitList<T> : List<T> where T : IFruit { ... } 
+10
source share

If you use generics, is there a purpose for creating a FruitList type? Could you just use List?

There won't be much difference in performance, so I say, why create three different classes when you do the same? Use a general solution.

+9
source share

It is much easier to maintain 1 general list than 3 non-general versions. If you really like the name AppleList, you can always use the trick to name a generic list.

 using AppleList=Fruit.FruitList<Fruit.Apple> 
+7
source share

Reuse common collection classes and subclasses of them only if you add additional functions. If possible, keep your subclass. This is the least complicated implementation.

+3
source share

Use a common list, there is no dot in the breakdown of 3 lists, and it is always useful to maintain the level of abstraction. (IFruit will be a good interface).

+1
source share

I would not recommend the accepted answer, and I think you meant something like this:

 public enum Fruit { Apple, Orange, Lemon } public interface IFruitList : IList<int> { Fruit Type { get; } }; public class FruitList : List<int>, IFruitList { private readonly type; FruitList(Fruit type) : base() { this.type = type; } FruitList(Fruit type, IEnumerable<int> collection) : base(collection) { this.type = type; } Fruit Type { return type; } } 
+1
source share

You should take YAGNI if you do not need it. Therefore, if you do not need to do more than you get on the list, just List<T> . If for some reason you need to redefine the list, create

 FruitList<T> : List<T> where T : Fruit 

If your lists are diverging and are no longer polymorphic, consider using your custom lists:

  • AppleList
  • OrangeList
  • LemonList

Try your best to keep your inheritance hierarchy as flat as possible to avoid overuse of your solution.

0
source share

All Articles