Enumerators

Enumerators

Enumerators (a.k.a. enumeration types, a.k.a. enums) make up a special subset of value types. All enumerators are derived from the [mscorlib]System.Enum class, which is the only class derived from [mscorlib]System.ValueType. Enumerators are possibly the most primitive of all types, and the rules regarding them are the most restrictive.

Unlike other value types in their boxed form, enumerators don’t show any of the characteristics of a “true class.” Enumerators can have only fields as members—no methods, properties, or events. Enumerators cannot implement interfaces; because enumerators cannot have methods, the question of implementing interfaces is moot.

Even with the fields the enumerators have no leeway: an enumerator must have exactly one instance field and at least one static field. The instance field of an enumerator represents the value of the current instance of the enumerator and must be of integer, Boolean, or string type. The type of the instance field is the underlying type of the enumerator. The enumerator itself as a value type is completely interchangeable with its underlying type in all operations except boxing. If an operation, other than boxing, expects a Boolean variable as its argument, a variable of a Boolean-based enumeration type can be used instead, and vice versa. A boxing operation, however, always results in a boxed enumerator and not in a boxed underlying type.

The static fields represent the values of the enumeration itself and have the type of the enumerator. As values of the enumeration, these fields must be not only static (shared by all instances of the enumerator) but also literal—they represent constants defined in the metadata. The literal fields are not true fields because they do not occupy memory allocated by the loader when the enumerator is loaded. (Chapter 8 discusses this and other aspects of fields.)

Generally speaking, you can think of an enumerator as a restriction of its underlying type to a predefined, finite set of values. As such, an enumerator obviously cannot have any specific layout requirements and must have the auto layout flag set.



Inside Microsoft. NET IL Assembler
Inside Microsoft .NET IL Assembler
ISBN: 0735615470
EAN: 2147483647
Year: 2005
Pages: 147
Authors: SERGE LIDIN

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net