C# 如何获取枚举的基础值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/97391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the underlying value of an enum
提问by George Mauer
I have the following enum declared:
我声明了以下枚举:
public enum TransactionTypeCode { Shipment = 'S', Receipt = 'R' }
How do I get the value 'S' from a TransactionTypeCode.Shipment or 'R' from TransactionTypeCode.Receipt ?
如何从 TransactionTypeCode.Shipment 或 TransactionTypeCode.Receipt 获取值“S”或“R”?
Simply doing TransactionTypeCode.ToString() gives a string of the Enum name "Shipment" or "Receipt" so it doesn't cut the mustard.
简单地执行 TransactionTypeCode.ToString() 会给出一个枚举名称“Shipment”或“Receipt”的字符串,因此它不会削减芥末。
采纳答案by J D OConal
Try this:
尝试这个:
string value = (string)TransactionTypeCode.Shipment;
回答by Andy
I believe Enum.GetValues() is what you're looking for.
我相信 Enum.GetValues() 是你正在寻找的。
回答by IaCoder
This is how I generally set up my enums:
这是我通常设置枚举的方式:
public enum TransactionTypeCode {
Shipment("S"),Receipt ("R");
private final String val;
TransactionTypeCode(String val){
this.val = val;
}
public String getTypeCode(){
return val;
}
}
System.out.println(TransactionTypeCode.Shipment.getTypeCode());
回答by IaCoder
I was Searching For That and i get the Solution Use the Convert Class
我正在寻找那个,我得到了解决方案使用转换类
int value = Convert.ToInt32(TransactionTypeCode.Shipment);
see how it easy
看看有多容易
回答by Andre
You have to check the underlying type of the enumeration and then convert to a proper type:
您必须检查枚举的基础类型,然后转换为正确的类型:
public enum SuperTasks : int
{
Sleep = 5,
Walk = 7,
Run = 9
}
private void btnTestEnumWithReflection_Click(object sender, EventArgs e)
{
SuperTasks task = SuperTasks.Walk;
Type underlyingType = Enum.GetUnderlyingType(task.GetType());
object value = Convert.ChangeType(task, underlyingType); // x will be int
}
回答by nawfal
The underlying type of your enum is still int, just that there's an implicit conversion from charto intfor some reason. Your enum is equivalent to
您的枚举的基础类型仍然是 int,只是出于某种原因存在从charto的隐式转换int。你的枚举相当于
TransactionTypeCode { Shipment = 83, Receipt = 82, }
Also note that enumcan have any integral type as underlying type except char, probably for some semantic reason. This is not possible:
另请注意,可能出于某种语义原因,enum可以将任何整数类型作为基础类型,除了char。这不可能:
TransactionTypeCode : char { Shipment = 'S', Receipt = 'R', }
To get the charvalue back, you can just use a cast.
要取回char价值,您只需使用演员表即可。
var value = (char)TransactionTypeCode.Shipment;
// or to make it more explicit:
var value = Convert.ToChar(TransactionTypeCode.Shipment);
The second one causes boxing, and hence should preform worse. So may be slightly better is
第二个会导致拳击,因此应该更糟。所以可能稍微好一点的是
var value = Convert.ToChar((int)TransactionTypeCode.Shipment);
but ugly. Given performance/readability trade-off I prefer the first (cast) version..
但丑。考虑到性能/可读性的权衡,我更喜欢第一个(演员)版本..
回答by Tonio
the underlying values of the enum has to be numeric. If the type of underlying values are known, then a simple cast returns the underlying value for a given instance of the enum.
枚举的基础值必须是数字。如果基础值的类型已知,那么简单的强制转换会返回给定枚举实例的基础值。
enum myEnum : byte {Some = 1, SomeMore, Alot, TooMuch};
myEnum HowMuch = myEnum.Alot;
Console.Writeline("How much: {0}", (byte)HowMuch);
OUTPUT: How much: 3
输出:多少:3
OR (closer to the original question)
或(更接近原始问题)
enum myFlags:int {None='N',Alittle='A',Some='S',Somemore='M',Alot='L'};
myFlags howMuch = myFlags.Some;
Console.WriteLine("How much: {0}", (char)howMuch);
//If you cast as int you get the ASCII value not the character.
This is recurring question for me, I always forget that a simple cast gets you the value.
这对我来说是一个反复出现的问题,我总是忘记一个简单的演员会给你带来价值。

