O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 25. Using enums

Java developers may be new to this keyword, though C/C++ users will find it very familiar. Enums are used frequently because it is a very convenient way to represent shared constants.

An enumeration is a collection of int elements. However, unlike other collections or arrays, enums are usually used to assign arbitrary int values to a series of strings, so that they can be used like constants. For example:

 1:  using System;
 2:
 3:  public class TestClass{
 4:    enum Month
 5:      {Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};
 6:
 7:    public static void Main(){
 8:      Console.WriteLine(Month.Jan);
 9:      Console.WriteLine(Month.Oct);
10:      Console.WriteLine((int)Month.Jan);
11:      Console.WriteLine((int)Month.Oct);
12:    }
13:  }

Output:

 c:\expt>test ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required