Generic question (Parameters of type vs Constructors)

advertisements

If you have a custom collection class that stores the weights of a single class of fruits individually in floats/double, like so:

1.1, 3.3, 6.6, 4.4, ...

and you would need to specify whether it's float or double and to differentiate the fruit type, is it better to do it like this:

Using an enum:

FruitList<float, FruitType.Orange>

or

Using an enum in the constructor:

FruitList<float> (FruitType.Orange)

or

Using a class:

FruitList<float, Orange>

or

Using a class in the constructor:

FruitList<float> (Orange)

Thing thing that confuses me is whether specifying these kind of stuff makes sense in the <> area (don't know what that area is called)?

Which is the better, faster, more efficient practice?

Btw the Orange class is never used anywhere, just wrote it if that would make sense to specify the type like that?

EDIT: Btw this is just an example case, not the actual one, but this reflects in a clear way. Like it's all floats/doubles and are passed to some other methods. But the methods need to know what type of fruit to perform correctly. The fruit themselves mean nothing.


First of all, enum values are not types, so the example

FruitList<float, FruitType.Orange>

won't work. If you want to ensure that your generic list is staticly typed to Orange you will have to use this one

FruitList<float, Orange>

even though you will never use the Orange class. As I see it, there probably isn't a need, since you can convey type in the variable name:

FruitList<float> orangeWeights = new FruitList<float>(FruitType.Orange);