C# Enumeration (enum) with Example

In this tutorial, we will learn about the enumeration (enum), how to declare and access enumeration in C#? By IncludeHelp Last updated : April 04, 2023

What is an enum (or enumeration type)?

The word enum stands for enumeration. An enum (or enumeration type) is a set of named integer constants. Enums default integer values start with 0, we can also set any other sequence of the values. An enum is defined by using the enum keyword.

Syntax to define an enum

enum enum_name {enumeration_list };

C# Enumeration (enum) Examples

Practice these programs to learn the concept of enumerations.

Example 1

Here, we are defining an enum named colors with the three constants RED, GREEN and BLUE, we are not initializing them with any value. Thus, constant will have values 0, 1 and 2.

using System;
using System.Text;

namespace Test {
  class Program {
    //declaring enum
    enum colors {
      RED,
      GREEN,
      BLUE
    };
    static void Main(string[] args) {
      //printing values
      Console.WriteLine("Red: {0}", (int) colors.RED);
      Console.WriteLine("Green: {0}", (int) colors.GREEN);
      Console.WriteLine("Blue: {0}", (int) colors.BLUE);

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}

Output

Red: 0
Green: 1
Blue: 2

Example 2

Here, we are defining an enum named colors with the three constants RED, GREEN and BLUE, we are initializing RED with 10, GREEN with 20 and BLUE is not getting initialized with any value, so it will take 21. Thus, constant will have values 10, 20 and 21.

using System;
using System.Text;

namespace Test {
  class Program {
    //declaring enum
    enum colors {
      RED = 10, GREEN = 20, BLUE
    };
    static void Main(string[] args) {
      //printing values
      Console.WriteLine("Red: {0}", (int) colors.RED);
      Console.WriteLine("Green: {0}", (int) colors.GREEN);
      Console.WriteLine("Blue: {0}", (int) colors.BLUE);

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}

Output

Red: 10
Green: 20
Blue: 21

Example 3: Printing enum's constant name and value using foreach loop

Here, we are using Enum.GetValues(typeof(Days) which returns an array of all enum values and to get the name of enum, we are using Enum.GetName(typeof(Days), day). Here, Days is the enum name and day is the index.

using System;
using System.Text;

namespace Test {
  class Program {
    //declaring enum
    enum Days {
      SUN,
      MON,
      TUE,
      WED,
      THU,
      FRE,
      SAT
    };
    static void Main(string[] args) {
      //printing enum using foreach loop
      foreach(int day in Enum.GetValues(typeof (Days))) {
        Console.WriteLine("Name: {0}, Value: {1}", Enum.GetName(typeof (Days), day), (int) day);
      }

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}

Output

Name: SUN, Value: 0
Name: MON, Value: 1
Name: TUE, Value: 2
Name: WED, Value: 3
Name: THU, Value: 4
Name: FRE, Value: 5
Name: SAT, Value: 6




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.