I have a business object which is basically a wrapper around a value of type int
. There are some constraints for the value/object:
- Not every value in
int
's range is valid1 - The valid values are not a predefined discrete set, therefore an
enum
is not an option - Two objects with the same value are always considered equal
- The validity of a value should be checked in the constructor
- 0 is not an valid value
If I just consider the first three oft theses constraints I'd say this is a predestined use case for an immutable struct
(which I would prefer). The problem lies within the last two ones:
Since I can't have a parameterless constructor in a struct
an object with 0 as a value can be constructed. I could treat this as a special value like a null
value. But this would force me to "null check" and I could take a class
as well. Are there any more reasons for using a struct
in this use case?
1 To be more precise at the valid values: they have to have 5 or 6 digits. The first 4 can have any value between 1000 and 9999, the remaining digit(s) are either between 1 and 4 or 1 and 12.