CA1028: Enum storage should be Int32

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName EnumStorageShouldBeInt32
CheckId CA1028
Category Microsoft.Design
Breaking Change Breaking

Cause

The underlying type of a public enumeration is not System.Int32.

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. Note that 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.

Example of a Violation

Description

The following example shows two enumerations that do not use the recommended underlying data type.

Code

using System;

namespace DesignLibrary
{
   [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
   }
}
Imports System

Namespace Samples

    <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

End Namespace

Example of How to Fix

Description

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

Code

using System;

namespace Samples
{
    [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
    }
}
Imports System

Namespace Samples

    <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

End Namespace

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

See Also

System.Byte System.Int16 System.Int32 System.Int64