C# Enumeration Example

  • C#
  • 1 min read

An enumeration is set of named constants of distinct value type called enumerator in C#. Every enumeration has an underlying type, which can be any data type like integer, long, short, etc. except for char type. The syntax of an enumeration in C# is the following:

Syntax

[attributes] [modifiers] enum identifier
[:base-type] {enumerator-list};

C# Enumeration Example

using System;

namespace EnumeratedConstants
{
    class EnumeratedConstants
    {
        enum Temperature
        {
            MaxCold = 0,
            MaxFreezingPoint = 31,
            NormalWeather = 50,
            SwimmingWeather = 62,
            BoilingPointWeather = 222,
        }
        static void Main(string[] args)
        {
            System.Console.WriteLine("Max Freezing point of water: {0}",
            (int)Temperature.MaxFreezingPoint);
            System.Console.WriteLine("Boiling point weather of water: {0}",
            (int)Temperature.BoilingPointWeather);
            Console.ReadLine();
        }
    }
}

Output

Max Freezing point of water: 31
Boiling point weather of water: 222

See also: