An enumeration has multiple members which are explicitly assigned the same constant value.
Rule descriptionEvery 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
}
When to suppress warnings
Do not suppress violations of this rule.
See alsoRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4