Obtenez la valeur int de l'enum dans C#

Mots clés : c#enumscastingintc#

meilleur 5 Réponses Obtenez la valeur int de l'enum dans C#

vote vote

93

int something = (int) Question.Role; 
enum StarsInMilkyWay:long {Sun = 1, V645Centauri = 2 .. Wolf424B = 2147483649}; 
long something = (long)StarsInMilkyWay.Wolf424B; 
vote vote

80

enum Sides {     Left, Right, Top, Bottom } Sides side = Sides.Bottom;  object val = Convert.ChangeType(side, side.GetTypeCode()); Console.WriteLine(val); 
vote vote

76

public static class Question {     public const int Role = 2;     public const int ProjectFunding = 3;     public const int TotalEmployee = 4;     public const int NumberOfServers = 5;     public const int TopBusinessConcern = 6; } 
vote vote

64

Enum e = Question.Role; 
int i = Convert.ToInt32(e); int i = (int)(object)e; int i = (int)Enum.Parse(e.GetType(), e.ToString()); int i = (int)Enum.ToObject(e.GetType(), e); 
vote vote

54

Question question = Question.Role; int value = (int) question; 

Questions similaires