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

25.1. Specifying different int values for enum elements

Although numbering starts from 0 by default, if you have a good reason for assigning each element in the enum a different int value, you can specify counting to start from any other value:

enum Month {Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};

In this case, Month.Jan will be 1, Month.Feb will be 2 and Month.Dec will be 12.

In fact, you can specify numbering for each int element in an enum, as this example shows: [2]

[2] There had better be a good reason for doing this!

 1: using System; 2: 3: public class TestClass{ 4: enum Month { 5: Jan=12, 6: Feb=5, 7: Mar=3, 8: Apr, // 4 9: May, // 5 10: Jun=9, 11: Jul, // 10 12: Aug, // 11 13: Sep, // 12 14: Oct, // 13 15: Nov=4, 16: Dec // ...

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