नोट
इस पृष्ठ तक पहुंच के लिए प्राधिकरण की आवश्यकता होती है। आप साइन इन करने या निर्देशिकाएँ बदलने का प्रयास कर सकते हैं।
इस पृष्ठ तक पहुंच के लिए प्राधिकरण की आवश्यकता होती है। आप निर्देशिकाएँ बदलने का प्रयास कर सकते हैं।
| 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 10 | As suggestion |
Cause
An enumeration has multiple members which are explicitly assigned the same constant value.
Rule description
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:
- Accidental typing mistakes, where the user accidentally typed the same constant value for multiple members.
- Copy paste mistakes, where the user copied an existing member definition, then renamed the member but forgot to change the value.
- Merge resolution from multiple branches, where a new member was added with a different name but the same value in different branches.
How to fix violations
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
}
When to suppress warnings
Do not suppress violations of this rule.