Is it preferable to explicitly set enum's fields instead of just defining their names? E.g. any pros and cons for Enum1 vs Enum2?
Enum1:
enum SomeEnum
{
Something1 = 0,
Something2 = 1
}
Enum2:
enum SomeEnum
{
Something1,
Something2
}
P.S. This question doesn't relates to enums that would be stored in database, which is requires to set values explicitly.
Edit: Say all values are 0, 1, etc... They are not negative or something.
Enum1 and Enum2 compile to the same enumeration. So from a programming standpoint, there is no difference. From a maintenance standpoint, there may or may not be issues.
On the one hand, declaring explicit values is absolutely required for a [Flags]
enum; and they need to be values at powers of 2.
On the other hand, explicit values can reduce maintainability simply by creating values that cannot or should not be changed in the future.
In general, I prefer to not declare explicit values unless the values themselves are significant for some reason -- if your C# enumeration is, for example, wrapping a C enumeration.
it's useful when you need to have negative values
enum
{
Error = -1,
Success = 0,
Something1,
Something2,
};
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