Free mag vol1 | Page 206

CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II Contractor, // = 104 VicePresident // = 105 } Enumerations do not necessarily need to follow a sequential ordering, and need not have unique values. If (for some reason or another) it makes sense to establish your EmpType as shown here, the compiler continues to be happy: // Elements of an enumeration need not be sequential! enum EmpType { Manager = 10, Grunt = 1, Contractor = 100, VicePresident = 9 } Controlling the Underlying Storage for an enum By default, the storage type used to hold the values of an enumeration is a System.Int32 (the C# int); however, you are free to change this to your liking. C# enumerations can be defined in a similar manner for any of the core system types (byte, short, int, or long). For example, if you want to set the underlying storage value of EmpType to be a byte rather than an int, you can write the following: // This time, EmpType maps to an underlying byte. enum EmpType : byte { Manager = 10, Grunt = 1, Contractor = 100, VicePresident = 9 } Changing the underlying type of an enumeration can be helpful if you are building a .NET application that will be deployed to a low-memory device (such as a Windows Phone 7 device) and need to conserve memory wherever possible. Of course, if you do establish your enumeration to use a byte as storage, each value must be within its range! For example, the following version of EmpType will result in a compiler error, as the value 999 cannot fit within the range of a byte: // Compile-time error! enum EmpType : byte { Manager = 10, Grunt = 1, Contractor = 100, VicePresident = 999 } 999 is too big for a byte! 141