Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve value of Enum based on index - c#

Tags:

c#

enums

This is my enum:

public enum DocumentTypes     {         [EnumMember]         TYPE_1 = 1,         [EnumMember]         TYPE_2 = 2,         [EnumMember]         TYPE_3 = 3,         [EnumMember]         TYPE_4 = 4,         [EnumMember]         TYPE_5 = 5,         [EnumMember]         TYPE_6 = 6,         [EnumMember]         TYPE_7 = 7,         [EnumMember]         TYPE_8 = 12      } 

If I want to obtain 'TYPE_8', if I only have 12, is there a way to get the enum value?

I tried this:

((DocumentTypes[])(Enum.GetValues(typeof(DocumentTypes))))[Convert.ToInt32("3")].ToString() 

which returns a value of 'TYPE_4'

like image 984
ltech Avatar asked Dec 17 '09 19:12

ltech


1 Answers

You can directly cast it:

int value = 12; DocumentTypes dt = (DocumentTypes)value; 
like image 86
Darin Dimitrov Avatar answered Sep 17 '22 18:09

Darin Dimitrov