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.
FruitList<float>(FruitType.Orange)
is probably the way to go.
float
cannot be anything except a type parameter. The fruit type is just a property. It doesn't make sense to create a separate type and use type parameters for it.
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);
Depends if that Orange of yours is just going to be like a... tag... like Color. A car can be blue and red and still be "the same" car. If you want the Orange to be able to do something special, something different than a plain fruit, or an apple, I create an abstract Fruit class and a sub-class Orange, and then use the <> thing. If just a tag thing, like a color, then an enum can do just fine I guess.
I would think it is easier to have type checking and such by using classes/structs though. Like, what would happen if someone tried to add some fruit of type apple to that fruitlist of yours? would it become an orange? would it have to check if the fruit type of the fruit was orange? what would happen if it was not? argumentexception?
I think I would go for classes and <> thingy. But that is me. Actually if it was me, I think I would just have gone ahead and used decimal
for the weight and everything else that needed to be measured... and then had something like (totally wrong syntax etc, but you get the idea hopefully):
class FruitList<TFruit> where TFruit : Fruit {}
abstract class Fruit {public abstract decimal Weight {get; protected set; }}
class Orange : Fruit { ... }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.