Underlying Types

Because we’ve mentioned the word constant quite a bit, you might be wondering where enum members get their values. Moreover, what’s the type of those constant values? To answer those questions, we must introduce the notion of an underlying type that’s associated with an enum’s definition. The underlying type indicates the domain for the constants declared within the enum. If left unspecified, an enum’s underlying type is a 32-bit integer. You can specify the underlying type explicitly as shown here:

enum Color : long{    Red,    Green,    Blue,}

You’ll see this colon “derives from” syntax recurring quite a bit in Chapter 14 when we discuss inheritance. Permitted underlying types are integral numeric types such as byte, short, ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.