Enums

An enum is a strongly typed set of constants that help make coding more meaningful and safe. Listing 2.17 shows how to declare and use an enum.

Listing 2.17 The enum Type (EnumType.cs)
 using System; class EnumType {    public enum CountEnum { One, Two, Three };    static void Main()    {       CountEnum myEnum = CountEnum.Two;       switch (myEnum)       {          case CountEnum.One:             Console.WriteLine(CountEnum.One);             break;          case CountEnum.Two:             Console.WriteLine(CountEnum.Two);             break;          default:             Console.WriteLine(myEnum);             break;       }       Console.ReadLine();    } } 

The CountEnum in Listing 2.17 is used in a switch statement, but can also be used anywhere in a program that needs to use a typed constant. The EnumType.EnumValue pattern is mandatory and can't be used as just EnumValue. For instance, CountEnum.Three cannot be abbreviated as just Three.



C# Builder KickStart
C# Builder KickStart
ISBN: 672325896
EAN: N/A
Year: 2003
Pages: 165

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