Q:

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

belongs to collection: C# Enum Class Programs

0

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

All Answers

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

Program:

The source code to demonstrate the use of Parse() method of Enum class is given below. The given program is compiled and executed successfully.

using System;

class Sample
{
    enum Directions { EAST=0,WEST=1,NORTH=2,SOUTH=3};
   
    //Entry point of Program
    static public void Main()
    {
        Directions dir;

        //Parse string to objects then 
        //we convert it to Enum objects
        dir  = (Directions)Enum.Parse(typeof(Directions), "1");
        Console.WriteLine(Enum.GetName(typeof(Directions),dir));

        //Parse string to objects then 
        //we convert it to Enum objects with ignore case
        dir = (Directions)Enum.Parse(typeof(Directions), "3",true);
        Console.WriteLine(Enum.GetName(typeof(Directions), dir));
    }
}

Output:

WEST
SOUTH
Press any key to continue . . .

 

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

total answers (1)

C# program to demonstrate the use of ToObject() me... >>
<< C# program to demonstrate the IsDefined() method o...