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 // ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.