Tuesday, September 27, 2011

value from the enum ?

Question:

      Is there any way that I can access the assigned value from the enum?

 

                Eg: When my item is Red then I want get the corresponding value That’s ‘R’. Is that possible?

public enum Color

      {

          Red = 'R',

          Blue = 'B',

          Green ='G',

          Yellow ='Y',

          White = 'W'

      }

 

Answer:

 

Yes by this way –

 

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine((char)Color.Red);

        }

    }

 

    public enum Color

      {

          Red = 'R',

          Blue = 'B',

          Green ='G',

          Yellow ='Y',

          White = 'W'

      }

 

No comments:

Post a Comment

Code Formater

Paste Here Your Source Code
Source Code Formatting Options
1) Convert Tab into Space :
2) Need Line Code Numbering :
3) Remove blank lines :
4) Embeded styles / Stylesheet :
5) Code Block Width :
6) Code Block Height :
7) Alternative Background :
Copy Formatted Source Code
 
Preview Of Formatted Code