Is there a way to use an integer index to return the appropriate value from an enum? For example, if there is the enum Color {Red, Green, Blue) is there a function that for the value 0 will return Red, 1 will return Green, and 2 will return Blue?
            Asked
            
        
        
            Active
            
        
            Viewed 6,666 times
        
    4 Answers
6
            The Enum.GetName method: http://msdn.microsoft.com/en-us/library/system.enum.getname.aspx
Using your example,
Console.WriteLine(Enum.GetName(typeof(Color), 1));
prints "Green"
 
    
    
        Chris Shain
        
- 50,833
- 6
- 93
- 125
- 
                    2Note that if you obfuscate your code, you will get jibberish instead of a human readable name. In that case, write your own translation method. – Brian Dishaw Aug 02 '11 at 01:05
2
            
            
        string color = ((Color)1).ToString(); //color is "Green"
Use the Enum.ToString() method.
 
    
    
        Evan Mulawski
        
- 54,662
- 15
- 117
- 144
 
     
    