संपादित करें

इसके माध्यम से साझा किया गया


CA1028: Enum storage should be Int32

Property Value
Rule ID CA1028
Title Enum storage should be Int32
Category Design
Fix is breaking or non-breaking Breaking
Enabled by default in .NET 8 No

Cause

The underlying type of an enumeration is not System.Int32.

By default, this rule only looks at externally visible enumerations, but this is configurable.

Rule description

An enumeration is a value type that defines a set of related named constants. By default, the System.Int32 data type is used to store the constant value. Even though you can change this underlying type, it is not necessary or recommended for most scenarios. No significant performance gain is achieved by using a data type that is smaller than Int32. If you cannot use the default data type, you should use one of the Common Language System (CLS)-compliant integral types, Byte, Int16, Int32, or Int64 to make sure that all values of the enumeration can be represented in CLS-compliant programming languages.

How to fix violations

To fix a violation of this rule, unless size or compatibility issues exist, use Int32. For situations where Int32 is not large enough to hold the values, use Int64. If backward compatibility requires a smaller data type, use Byte or Int16.

When to suppress warnings

Suppress a warning from this rule only if backward compatibility issues require it. In applications, failure to comply with this rule usually does not cause problems. In libraries, where language interoperability is required, failure to comply with this rule might adversely affect your users.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable CA1028
// The code that's violating the rule is on this line.
#pragma warning restore CA1028

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA1028.severity = none

For more information, see How to suppress code analysis warnings.

Configure code to analyze

Use the following option to configure which parts of your codebase to run this rule on.

You can configure this option for just this rule, for all rules it applies to, or for all rules in this category (Design) that it applies to. For more information, see Code quality rule configuration options.

Include specific API surfaces

You can configure which parts of your codebase to run this rule on, based on their accessibility. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.CAXXXX.api_surface = private, internal

Example

The following example shows two enumerations that don't use the recommended underlying data type.

[Flags]
public enum Days : uint
{
    None = 0,
    Monday = 1,
    Tuesday = 2,
    Wednesday = 4,
    Thursday = 8,
    Friday = 16,
    All = Monday | Tuesday | Wednesday | Thursday | Friday
}

public enum Color : sbyte
{
    None = 0,
    Red = 1,
    Orange = 3,
    Yellow = 4
}
<Flags()>
Public Enum Days As UInteger
    None = 0
    Monday = 1
    Tuesday = 2
    Wednesday = 4
    Thursday = 8
    Friday = 16
    All = Monday Or Tuesday Or Wednesday Or Thursday Or Friday
End Enum

Public Enum Color As SByte
    None = 0
    Red = 1
    Orange = 3
    Yellow = 4
End Enum

The following example fixes the previous violation by changing the underlying data type to Int32.

[Flags]
public enum Days : int
{
    None = 0,
    Monday = 1,
    Tuesday = 2,
    Wednesday = 4,
    Thursday = 8,
    Friday = 16,
    All = Monday | Tuesday | Wednesday | Thursday | Friday
}

public enum Color : int
{
    None = 0,
    Red = 1,
    Orange = 3,
    Yellow = 4
}
<Flags()>
Public Enum Days As Integer
    None = 0
    Monday = 1
    Tuesday = 2
    Wednesday = 4
    Thursday = 8
    Friday = 16
    All = Monday Or Tuesday Or Wednesday Or Thursday Or Friday
End Enum

Public Enum Color As Integer
    None = 0
    Red = 1
    Orange = 3
    Yellow = 4
End Enum

See also