ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

Here, we are going to learn how to print the name of enum constant based on an integer value in C#?
Submitted by Nidhi, on November 08, 2020

Here, we will get the name of enum constant based on integer value using the GetName() method of the Enum class.

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.

C# Enum Class Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.