得到枚举int值 [英] Get int value from enum

查看:163
本文介绍了得到枚举int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为问题(复数)类。在这个类中有一个名为问题(单数)枚举它看起来像这样。

I have a class called Questions (plural). In this class there is an enum called Question (singular) which looks like this.

public enum Question
{
    Role = 2,
    ProjectFunding = 3,
    TotalEmployee = 4,
    NumberOfServers = 5,
    TopBusinessConcern = 6
}

问题类我有一个返回 GET(INT富)功能>问题对象中。有没有一种简单的方法来获取整数关枚举,所以我可以做这样的事情 Questions.Get(Question.Role)

In the Questions class I have a get(int foo) function that returns a Questions object for that foo. Is there an easy way to get the integer value off the enum so I can do something like this Questions.Get(Question.Role)?

推荐答案

只投枚举如:

int something = (int)Question.Role;

以上将绝大多数的枚举你在野外看到的工作,作为默认基础类型的枚举类型为int。

The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int.

然而,随着cecilphillip指出,枚举可以有不同的基础类型。
如果枚举被声明为UINT,或长或ULONG那么就应该转换为枚举类型。

However as cecilphillip points out, enums can have different underlying types. If an enum is declared as an uint, long or ulong then it should be cast to the type of the enum.

例如。为

enum StarsInMilkyWay:long {Sun = 1, V645Centauri = 2 .. Wolf424B = 2147483649};

你应该使用

long something = (long)StarsInMilkyWay.Wolf424B;

这篇关于得到枚举int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆