Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 8 Language Overviewprevious at this levelnext at this level 8.11 EnumsAn enum type declaration defines a type name for a related group of symbolic constants. Enums 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 example
enum Color   
{  
   Red,  
   Blue,  
   Green  
}  
class Shape  
{  
   public void Fill(Color color) {  
      switch(color) {  
         case Color.Red:  
         ...  
         break;  
         case Color.Blue:  
         ...  
         break;  
         case Color.Green:  
         ...  
         break;  
         default:  
         break;  
      }  
   }  
}  
shows a Color enum and a method that uses this enum. The signature of the Fill method makes it clear that the shape can be filled with one of the given colors.
The use of enums is superior to the use of integer constants-as is common in languages without enums-because the use of enums 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. For example, the use of Color rather than int for a parameter type enables smart code editors to suggest Color values.
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS