


✔️ DO apply the System.FlagsAttribute to flag enums. For example, in C# the enum keyword is used to define an enumeration. Most programming languages provide a programming element that gives you access to this functionality.

System.Enum is a special type used by the CLR to create user-defined enumerations. ✔️ DO name flag enums with plural nouns or noun phrases and simple enums with singular nouns or noun phrases. You expect a large number of instances of the enum to be serialized.įor in-memory usage, be aware that managed objects are always DWORD-aligned, so you effectively need multiple enums or other small structures in an instance to pack a smaller enum with in order to make a difference, because the total instance size is always going to be rounded up to a DWORD. You expect users to create large arrays or collections of the enum instances. You expect the enum to be used as a field in a very frequently instantiated structure or class. The size savings might be significant if: If you expect the enum to be used mainly as an argument for flow of control, the size makes little difference.
CREATE ENUM ROW IN SEQUEL PRO CODE
The underlying type needs to be different than Int32 for easier interoperability with unmanaged code expecting different-size enums.Ī smaller underlying type would result in substantial savings in space. The enum is a flags enum and you have more than 32 flags, or expect to have more in the future. ✔️ CONSIDER using Int32 (the default in most programming languages) as the underlying type of an enum unless any of the following is true: ✔️ DO provide a value of zero on simple enums.Ĭonsider calling the value something like "None." If such a value is not appropriate for this particular enum, the most common default value for the enum should be assigned the underlying value of zero. They are used to track the state of the enum rather than being one of the values from the set represented by the enum. ❌ DO NOT include sentinel values in enums.Īlthough they are sometimes helpful to framework developers, sentinel values are confusing to users of the framework. Method overloading allows adding parameters in future releases. Such reserved parameters can be expressed as enums with a single default value. ❌ AVOID publicly exposing enums with only one value.Ī common practice for ensuring future extensibility of C APIs is to add reserved parameters to method signatures. Reserved values just pollute the set of real values and tend to lead to user errors. See Adding Values to Enums for more details on adding values to enums. You can always simply add values to the existing enum at a later stage. ❌ DO NOT provide reserved enum values that are intended for future use. ❌ DO NOT use an enum for open sets (such as the operating system version, names of your friends, etc.). ✔️ DO favor using an enum instead of static constants. ✔️ DO use an enum to strongly type parameters, properties, and return values that represent sets of values. A common example of the flags enum is a list of options. A common example of the simple enum is a set of colors.įlag enums are designed to support bitwise operations on the enum values. Simple enums represent small closed sets of choices. There are two kinds of enums: simple enums and flag enums.
