Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Property | Value |
---|---|
Rule ID | CA1069 |
Title | Enums should not have duplicate values |
Category | Design |
Fix is breaking or non-breaking | Breaking |
Enabled by default in .NET 9 | As suggestion |
An enumeration has multiple members which are explicitly assigned the same constant value.
Every enum member should either have a unique constant value or be explicitly assigned with a prior member in the enum to indicate explicit intent of sharing value. For example:
enum E
{
Field1 = 1,
AnotherNameForField1 = Field1, // This is fine
Field2 = 2,
Field3 = 2, // CA1069: This is not fine. Either assign a different constant value or 'Field2' to indicate explicit intent of sharing value.
}
This rule helps in catching functional bugs introduced from the following scenarios:
To fix a violation, either assign a new unique constant value or assign with a prior member in the enum to indicate explicit intent of sharing the same value. For example, the following code snippet shows a violation of the rule and couple of ways to fix the violation:
enum E
{
Field1 = 1,
AnotherNameForField1 = Field1, // This is fine
Field2 = 2,
Field3 = 2, // CA1069: This is not fine. Either assign a different constant value or 'Field2' to indicate explicit intent of sharing value.
}
enum E
{
Field1 = 1,
AnotherNameForField1 = Field1, // This is fine
Field2 = 2,
Field3 = 3, // This is now fine
}
enum E
{
Field1 = 1,
AnotherNameForField1 = Field1, // This is fine
Field2 = 2,
Field3 = Field2, // This is also fine
}
Do not suppress violations of this rule.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register now