A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1069 below:

CA1069: Enums should not have duplicate values (code analysis) - .NET

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 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:

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.

See also

RetroSearch 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