C# Program to Print the Integer Values of Enum Constants

You are currently viewing C# Program to Print the Integer Values of Enum Constants

C# program to print the integer values of enum constants

//C# program to print the integer value of enum constants.

using System;

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

class EnumDemo
{
    static void PrintColor(COLOR color)
    {
        switch (color)
        { 
            case COLOR.RED:
                Console.WriteLine("Color is Red,    value: " +(int)COLOR.RED);
                break;
            case COLOR.GREEN:
                Console.WriteLine("Color is Green,  value: " + (int)COLOR.GREEN);
                break;
            case COLOR.YELLOW:
                Console.WriteLine("Color is Yellow, value: " + (int)COLOR.YELLOW);
                break;
            case COLOR.BLACK:
                Console.WriteLine("Color is Black,  value: " + (int)COLOR.BLACK);
                break;
            case COLOR.WHITE:
                Console.WriteLine("Color is White,  value: " + (int)COLOR.WHITE);
                break;
            case COLOR.BLUE:
                Console.WriteLine("Color is Blue,   value: " + (int)COLOR.BLUE);
                break;
        }
    }
    static void Main(string[] args)
    {
        PrintColor(COLOR.RED    );
        PrintColor(COLOR.GREEN  );
        PrintColor(COLOR.BLUE   );
    }
}
Color is Red,    value: 0
Color is Green,  value: 1
Color is Blue,   value: 5
Press any key to continue . . .

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply