Created
November 26, 2013 15:21
-
-
Save tugberkugurlu/7660247 to your computer and use it in GitHub Desktop.
Get Enum names and values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum MyEnum : byte | |
{ | |
A = 0, | |
B = 1, | |
C = 2 | |
} | |
Array enumNames = Enum.GetNames(typeof(MyEnum)); | |
Array enumValues = Enum.GetValues(typeof(MyEnum)); | |
for (int i = 0; i < enumValues.Length; i++) | |
{ | |
Type underlyingType = Enum.GetUnderlyingType(typeof(MyEnum)); | |
object value = enumValues.GetValue(i); | |
object convertedValue = Convert.ChangeType(value, underlyingType); | |
Console.WriteLine(enumNames.GetValue(i).ToString() + ": " + convertedValue.ToString()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment