Skip to content

Latest commit

 

History

History
101 lines (69 loc) · 4.62 KB

File metadata and controls

101 lines (69 loc) · 4.62 KB
title description ms.date f1_keywords helpviewer_keywords author ms.author dev_langs
CA1028: Enum storage should be Int32 (code analysis)
Learn about code analysis rule CA1028: Enum storage should be Int32
03/11/2019
CA1028
EnumStorageShouldBeInt32
EnumStorageShouldBeInt32
CA1028
gewarren
gewarren
CSharp
VB

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 xref:System.Int32?displayProperty=fullName.

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 xref:System.Int32?displayProperty=fullName 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 xref:System.Int32. If you cannot use the default data type, you should use one of the Common Language System (CLS)-compliant integral types, xref:System.Byte, xref:System.Int16, xref:System.Int32, or xref:System.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 xref:System.Int32. For situations where xref:System.Int32 is not large enough to hold the values, use xref:System.Int64. If backward compatibility requires a smaller data type, use xref:System.Byte or xref:System.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.

[!INCLUDEapi-surface]

Example

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

:::code language="csharp" source="snippets/csharp/all-rules/ca1028.cs" id="snippet1":::

:::code language="vb" source="snippets/vb/all-rules/ca1028-enum-storage-should-be-int32_1.vb" id="snippet1":::

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

:::code language="csharp" source="snippets/csharp/all-rules/ca1028.cs" id="snippet2":::

:::code language="vb" source="snippets/vb/all-rules/ca1028-enum-storage-should-be-int32_1.vb" id="snippet2":::

Related rules

See also

  • xref:System.Byte?displayProperty=fullName
  • xref:System.Int16?displayProperty=fullName
  • xref:System.Int32?displayProperty=fullName
  • xref:System.Int64?displayProperty=fullName