In .NET
can you have multiple enum
values for the same integer?
eg.
public enum PersonGender
{
Unknown = 0,
Male = 1,
Female = 2,
Intersex = 3,
Indeterminate = 3,
NonStated = 9,
InadequatelyDescribed = 9
}
CA1069: Enums should not have duplicate values.
The values assigned to the enum names must be integral constant, i.e., it should not be of other types such string, float, etc. All the enum names must be unique in their scope, i.e., if we define two enum having same scope, then these two enums should have different enum names otherwise compiler will throw an error.
The answer is no. You cannot store more that one value in an ENUM column.
Enums type can be an integer (float, int, byte, double etc.) but if you use beside int, it has to be cast. Enum is used to create numeric constants in . NET framework.
In C#, this is allowed, as per the C# Language Specication, version 4. Section 1.10 Enums
doesn't explicitly mention the possibility but, later on in section 14 Enums, 14.3
, we see:
Multiple
enum
members may share the same associated value. The exampleenum Color { Red, Green, Blue, Max = Blue }
shows an
enum
in which twoenum
members -Blue
andMax
- have the same associated value.
That works fine. There is absolutely nothing wrong with the code you posted. It compiles just fine and works in code, with the caveat that
PersonGender.NonStated == PersonGender.InadequatelyDescribed
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