Base Type for Enums

Problem
You want to know when it’s okay to specify the base type for an enumeration.

Solution
As a general rule, you should avoid specifying a base type for enums. Instead, rely on the default Int32 base type.

Comments
There is an exception, of course. You may have a justified reason to specify a non-default base type. For example, let’s say you plan on creating large arrays of enum values. In this case, it’s perfectly valid to use Byte or Int16 as the base type. Just be sure, however, to never use a non-CLS-compliant integer type, like UInt32 or UInt64, as the base type.

Example

// Using a 16-bit enum to save memory.
public enum Authorization : short {
    Pending,
    Processing,
    Complete
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s