s

C# enum edit button Edit

author
Rakesh Kumar Sutar | calendar 19 January 2021 | 1988

Introduction

Enumeration (or enum) is a value data type in C#. It is mainly used to assign the names or string values to integral constants, that make a program easy to read and maintain. The main objective of enum is to define our own data types(Enumerated Data Types). Enumeration is declared using enum keyword directly inside a namespace, class, or structure.

Syntax

enum Enum_variable
{
     string_1...;
     string_2...;
     .
     .
}

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello_World
{
    enum month
    {

        // following are the data members 
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday,
        Sunday

    }
    class Program
    {
        static void Main(String[] args)
        {
            // getting the integer values of data members.. 
            Console.WriteLine("The value of Monday in week "   "enum is "   (int)month.Monday);
            Console.WriteLine("The value of Tuesday in week "   "enum is "   (int)month.Tuesday);
            Console.WriteLine("The value of Wednesday in week "   "enum is "   (int)month.Wednesday);
            Console.WriteLine("The value of Thursday in week "   "enum is "   (int)month.Thursday);
            Console.WriteLine("The value of Friday in week "   "enum is "   (int)month.Friday);
            Console.WriteLine("The value of Saturday in week "   "enum is "   (int)month.Saturday);
            Console.WriteLine("The value of Sunday in week "   "enum is "   (int)month.Sunday);
            Console.ReadLine();
        }
    }
}

Output