Q:

C# program to print the name of enum constant based on an integer value

belongs to collection: C# Enum Class Programs

0

C# program to print the name of enum constant based on an integer value

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to print the name of enum constant based on integer value is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to print the name of enum based on an integer value.

using System;

enum COLOR 
{
    RED,GREEN,YELLOW,BLACK,WHITE, BLUE
}

class EnumDemo
{
   
    static void Main(string[] args)
    {
        Console.WriteLine("Name is: {0}", Enum.GetName(typeof(COLOR), 2));
        Console.WriteLine("Name is: {0}", Enum.GetName(typeof(COLOR), 5)); 
    }
}

Output:

Name is: YELLOW
Name is: BLUE
Press any key to continue . . .

Explanation:

In the above program, we created an enum COLOR that contain constants with color names. Here, we also created a class EnumDemo that contains the Main() method. The Main() method is the entry point for the program.

In the Main() method, we called to get the name of enum constants using the GetName() method of Enum class and print the name on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# program to get the hash-code of enum constants... >>
<< C# program to get the type-code of enum constants...