Enums should have zero value
Collapse the table of content
Expand the table of content

Enums should have zero value







Breaking Change

NonBreaking, Breaking

An enumeration without an applied System.FlagsAttribute does not define a member with a value of zero; or an enumeration with an applied FlagsAttribute defines a member with a value of zero but its name is not 'None', or the enumeration defines multiple zero-valued members.

The default value of an un-initialized enumeration, like other value types, is zero. A non-flags attributed enumeration should define a member with the value of zero so that the default value is a valid value of the enumeration. If appropriate, name the member 'None'. Otherwise, assign zero to the most commonly used member. Note that if the value of the first enumeration member is not set in the declaration, its value is zero by default.

If an enumeration that has the FlagsAttribute applied defines a zero-valued member, its name should be 'None' to indicate that no values have been set in the enumeration. Using a zero-valued member for any other purpose is contrary to the use of the FlagsAttribute in that the AND and OR bitwise operators are useless with the member. This implies that only one member should be assigned the value zero. Note that if there are multiple members with the value zero in a flags-attributed enumeration, Enum.ToString() returns incorrect results for members that are not zero.

To fix a violation of this rule for non-flags attributed enumerations define a member with the value of zero; this is a non-breaking change. For flags-attributed enumerations that define a zero-valued member, name this member 'None' and delete any other members with a value of zero; this is a breaking change.

Do not exclude a warning from this rule except for flags-attributed enumerations that have previously shipped.

The following example shows two enumerations that satisfy the rule and an enumeration, BadTraceOptions, which violates the rule.

Imports System

Namespace DesignLibrary

   Public Enum TraceLevel
      Off     = 0
      AnError = 1
      Warning = 2
      Info    = 3
      Verbose = 4
   End Enum

   <Flags> _
   Public Enum TraceOptions
      None         =    0
      CallStack    = &H01
      LogicalStack = &H02
      DateTime     = &H04
      Timestamp    = &H08
   End Enum

   <Flags> _
   Public Enum BadTraceOptions
      CallStack    =    0
      LogicalStack = &H01
      DateTime     = &H02
      Timestamp    = &H04
   End Enum

   Class UseBadTraceOptions

      Shared Sub Main()

         ' Set the flags.
         Dim badOptions As BadTraceOptions = _
            BadTraceOptions.LogicalStack Or BadTraceOptions.Timestamp

         ' Check whether CallStack is set.
         If((badOptions And BadTraceOptions.CallStack) = _
            ' This 'If' statement is always true.
         End If

      End Sub
   End Class

End Namespace

Community Additions

© 2016 Microsoft