Type-Safe enumerations in Visual J# .NET 2005

An enumeration type defines a type name for a related group of symbolic constants. Enumerations are used for “multiple choice” scenarios, in which a runtime decision is made from a fixed number of choices that are known at compile-time. The use of enumerations is superior to the use of integer constants (or classes with static final fields) - as is common in languages without the notion of enumerations - because the use of enumerations makes the code more readable and self-documenting. The self-documenting nature of the code also makes it possible for the development tool to assist with code writing and other “designer” activities. Also, the fact the enumerations are value types renders them lightweight.

Though there are patterns to write type-safe enums they are verbose. For example, to do an enum in a language like C, or C++, or C#, user says enum Color {Red, Green, Blue }. Whereas while using a typesafe enum coding pattern one would need a lot more code, it would have to be a regular class (and therefore much heavier). Providing it as a language feature allows the programmer to make his intent very clear. They are typesafe, and the fact the enumerations are value types, renders them lightweight. They are as performant as the primitive integral type - that is important; J# ensured that the added advantage of expressivity did not come at the expense of performance. And, users can use them as the control type in a switch statement too.

 

You may read more about user defined Enumeration types in detail here - https://msdn2.microsoft.com/en-us/library/t2csdd3h.