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 System.Int32. If you cannot use the default data type, you should use one of the Common Language System (CLS)-compliant integral types, System.Byte, System.Int16, System.Int32, or 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 System.Int32. For situations where System.Int32 is not large enough to hold the values, use System.Int64. If backward compatibility requires a smaller data type, use System.Byte or 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 CA1028To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA1028.severity = noneFor 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, by setting the api_surface option. 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, internalExample
The following example shows two enumerations that don't use the recommended underlying data type.
sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1028.cssnippet1
using System;
namespace ca1028
{
//<snippet1>
[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
}
//</snippet1>
}
namespace ca1028_fix
{
//<snippet2>
[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
}
//</snippet2>
}sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1028-enum-storage-should-be-int32_1.vbsnippet1
Imports System
Namespace ca1028
'<snippet1>
<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
'</snippet1>
End Namespace
Namespace ca1028_2
'<snippet2>
<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
'</snippet2>
End NamespaceThe following example fixes the previous violation by changing the underlying data type to System.Int32.
sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1028.cssnippet2
using System;
namespace ca1028
{
//<snippet1>
[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
}
//</snippet1>
}
namespace ca1028_fix
{
//<snippet2>
[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
}
//</snippet2>
}sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1028-enum-storage-should-be-int32_1.vbsnippet2
Imports System
Namespace ca1028
'<snippet1>
<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
'</snippet1>
End Namespace
Namespace ca1028_2
'<snippet2>
<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
'</snippet2>
End NamespaceRelated rules
- CA1008: Enums should have zero value
- CA1027: Mark enums with FlagsAttribute
- CA2217: Do not mark enums with FlagsAttribute
- CA1700: Do not name enum values 'Reserved'
- CA1712: Do not prefix enum values with type name