C# Program to Demonstrate the Getname() Method of Enum Class

You are currently viewing C# Program to Demonstrate the Getname() Method of Enum Class

C# program to demonstrate the GetName() method of Enum class

using System;

class Sample
{
    enum Color { RED=0,GREEN=1,YELLOW=3,WHITE=4,BLACK=5};

    //Entry point of Program
    static public void Main()
    {
        string enumName = "";

        enumName = Enum.GetName(typeof(Color), 4);
        if (enumName == null)
            Console.WriteLine("Name is not available in ENUM");
        else
            Console.WriteLine("Name : " + enumName);

        enumName = Enum.GetName(typeof(Color), 6);
        if (enumName == null)
            Console.WriteLine("Name is not available in ENUM");
        else
            Console.WriteLine("Name : " + enumName);        
    }
}
Name : WHITE
Name is not available in ENUM
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