Using Enumeration in C#

Submitted by: 
Language: 

Objective

Many things in the real-world conceptually represent enumerations: the days of the
week, months of the year, seasons, oceans, and compass directions, to name a few. In this tutorial we will show you how to use Enumeration in C# language.

Let's go
Definition of Enumeration

Enumeration (or just an enum for short), is very important in C# language. It mirrors the nature of the world in the programming language. See if you want to describe a week with all days of it, the easy way is to declare a enumeration with all days that belong to it.

  1. public enum Week
  2. {
  3. Sunday,
  4. Monday,
  5. Tuesday,
  6. Wednesday,
  7. Thursday,
  8. Friday,
  9. Saturday
  10. }

In the above code, we declare a enumeration named Week and seven fields in that enumeration, each is separated by a comma. You can write many of fields in the body of an enum with no restrict.
When you want to refer to a field value in the enumeration, you just refer it directly from the enumeration name . Let see the example bellow:

  1. public void printDaysOfWeek(Week value_of_day)
  2. {
  3. switch(value_of_day){
  4. case Days.Monday:
  5. Console.WriteLine("This is Monday");
  6. break;
  7. case Days.Tuesday:
  8. Console.WriteLine("This is Tuesday");
  9. break;
  10. case Days.Wednesday:
  11. Console.WriteLine("This is Wednesday");
  12. break;
  13. case Days.Thursday:
  14. Console.WriteLine("This is Thursday");
  15. break;
  16. case Days.Friday:
  17. Console.WriteLine("This is Friday");
  18. break;
  19. case Days.Saturday:
  20. Console.WriteLine("This is Saturday");
  21. break;
  22. case Days.Sunday:
  23. Console.WriteLine("This is Sunday");
  24. break;
  25. default:
  26. Console.WriteLine("This is not a day");
  27. break;
  28.  
  29. }
  30. }

In this example we have a function named printDaysOfWeek with an input parameter named value_of_day. This input parameter is a Days enumeration type. In body of function, we compare the input value with each of value of enumeration. If true, so we print to console.

Enumeration with Multiple Named Values

In Enumeration, you can declare multiple named with same value. The following snippet code declares two fileds named Wednesday and DupWednesday that have the same value.

  1. public enum WeekWithMultipleNamedValues
  2. {
  3. Sunday,
  4. Monday,
  5. Tuesday,
  6. Wednesday,
  7. DupWednesday = Wednesday,//the HumpDay field has same value with Wednesday
  8. Thursday,
  9. Friday,
  10. Saturday
  11. }

By default, the compiler will set the first filed value of Enumeration is 0, and the next field value is increased sequentially. You can make your enumeration more clearly by assigning the value to each field:

  1. public enum Week
  2. {
  3. Sunday = 0,
  4. Monday = 1,
  5. Tuesday = 2,
  6. Wednesday = 3,
  7. Thursday = 4,
  8. Friday = 5,
  9. Saturday = 6,
  10. }

Underlying Enumeration Types

By default, compiler makes the value type of each fields is int (integer) as a default type. If you want to change that default type, you can declare target type directly after the name of Enumeration. Note that you can only specify some types of enumeration include byte, short, int, long, sbyte, ushort, uint, or ulong. Bellow is an example of how to change default type:

  1. enum Week: byte
  2. {
  3. Sunday,
  4. Monday,
  5. Tuesday,
  6. Wednesday,
  7. Thursday,
  8. Friday,
  9. Saturday,
  10. }

Base on the type of enumeration, the compiler will specify the range of value that match the type of enumeration field. For example, this above enumeration will have range of value from 0 to 255 for each value of field.

Summary

In this tutorial we have show you about enumeration, includes:

  • How to declare a enumeration
  • Multiple named value in enumeration
  • How to change type of enumeration

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.