C# 为什么我们不能有“char”枚举类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/572963/
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
Why we can't have "char" enum types
提问by Praveen Sharma
I want to know why we can't have "char" as underlying enum type. As we have byte,sbyte,int,uint,long,ulong,short,ushort as underlying enum type. Second what is the default underlying type of an enum?
我想知道为什么我们不能将“char”作为基础枚举类型。因为我们有 byte,sbyte,int,uint,long,ulong,short,ushort 作为底层枚举类型。其次,枚举的默认基础类型是什么?
采纳答案by tvanfosson
The default type is int. More information at the C# referenceat MSDN. You can also find a link to the C# language specificationat MSDN. I think the reason for the restriction probably derives from these statements in the language specification, section 4.1.5.
默认类型是 int。MSDN上的 C#参考中的更多信息。您还可以在 MSDN 上找到指向C# 语言规范的链接。我认为限制的原因可能来自语言规范第 4.1.5 节中的这些陈述。
The char type is classified as an integral type, but it differs from the other integral types in two ways:
? There are no implicit conversions from other types to the char type. In particular, even though the sbyte, byte, and ushort types have ranges of values that are fully representable using the char type, implicit conversions from sbyte, byte, or ushort to char do not exist.
? Constants of the char type must be written as character-literals or as integer-literals in combination with a cast to type char. For example, (char)10 is the same as '\x000A'.
char 类型被归类为整型,但它与其他整型有两点不同:
? 没有从其他类型到 char 类型的隐式转换。特别是,即使 sbyte、byte 和 ushort 类型具有可以使用 char 类型完全表示的值范围,也不存在从 sbyte、byte 或 ushort 到 char 的隐式转换。
? char 类型的常量必须写为字符字面量或整数字面量,并结合转换为 char 类型。例如,(char)10 与 '\x000A' 相同。
回答by Jeffrey Cameron
Character enums would simply be strings wouldn't they? I'm not sure what other benefit you would derive from a character enumeration?
字符枚举只是字符串,不是吗?我不确定您会从字符枚举中获得什么其他好处?
As others have said, the default type is int for an enumeration.
正如其他人所说,枚举的默认类型是 int 。
回答by Gregory A Beamer
Technically, you can't do this. But, you can convert the enum to a byte and then convert that to char. This is useful if your goal is to have something like this (realizing this is impossible to do:
从技术上讲,你不能这样做。但是,您可以将枚举转换为字节,然后将其转换为字符。如果您的目标是拥有这样的东西,这很有用(意识到这是不可能做到的:
public enum CharEnum
{
one = '1'
}
You can do this, however, by using ASCII byte values and then converting:
但是,您可以通过使用 ASCII 字节值然后转换来做到这一点:
public enum CharEnum
{
one = 49,
two = 50
}
You can then convert to byte and to char to get the char value. It is not really pretty, but it will work, if getting a char is your ultimate goal. You can also use unicode and an int value, if you need chars outside of the ASCII range. :-)
然后您可以转换为字节和字符以获取字符值。它不是很漂亮,但它会起作用,如果获得一个字符是你的最终目标。如果您需要 ASCII 范围之外的字符,您还可以使用 unicode 和 int 值。:-)
回答by Emir
This is workaround I'm using
这是我正在使用的解决方法
enum MyEnum
{
AA = 'A',
BB = 'B',
CC = 'C'
};
static void Main(string[] args)
{
MyEnum e = MyEnum.AA;
char value = (char)e.GetHashCode(); //value = 'A'
}
回答by kad81
I know this is an older question, but this information would have been helpful to me:
我知道这是一个较旧的问题,但这些信息对我有帮助:
It appears that there is no problem using char as the value type for enums in C# .NET 4.0 (possibly even 3.5, but I haven't tested this). Here's what I've done, and it completely works:
似乎在 C# .NET 4.0(甚至可能是 3.5,但我没有测试过)中使用 char 作为枚举的值类型没有问题。这是我所做的,它完全有效:
public enum PayCode {
NotPaid = 'N',
Paid = 'P'
}
Convert Enum to char:
将枚举转换为字符:
PayCode enumPC = PayCode.NotPaid;
char charPC = (char)enumPC; // charPC == 'N'
Convert char to Enum:
将字符转换为枚举:
char charPC = 'P';
if (Enum.IsDefined(typeof(PayCode), (int)charPC)) { // check if charPC is a valid value
PayCode enumPC = (PayCode)charPC; // enumPC == PayCode.Paid
}
Works like a charm, just as you would expect from the char type!
就像你对 char 类型所期望的那样,就像一个魅力!
回答by laurian
Pretty much everything has been said already about that. Just wanted to say you can use extensions if you use a lot of "char enums":
几乎所有的事情都已经说过了。只是想说,如果您使用大量“字符枚举”,则可以使用扩展:
public enum MyEnum
{
MY_VALUE = 'm'
}
public static class MyExtensions
{
public static char GetChar(this Enum value)
{
return (char)value.GetHashCode();
}
}
class Program
{
public static void Main(string[] args)
{
MyEnum me = MyEnum.MY_VALUE;
Console.WriteLine(me + " = " + me.GetChar());
}
}
回答by Mike
char charPC = 'P';
if (Enum.IsDefined(typeof(PayCode), (PayCode)charPC)) {
// check if charPC is a valid value
PayCode enumPC = (PayCode)charPC; // enumPC == PayCode.Paid
}
- Made a small change, no need to convert it to an (int) in the if for all cases, depends on the enum type. But if you cast it directly to the enum type itself, it should always work.
- 做了一个小改动,在所有情况下都不需要将它转换为 ( int),这取决于枚举类型。但是如果你直接将它转换为枚举类型本身,它应该总是有效的。
回答by user34660
See ECMA standard 335, Common Language Infrastructure (CLI), in Ecma International. The CLI allows the underlying type to be char or bool but C# and VB.Net don't allow it. For what it is worth, C++/CLI does allow System::Char as the underlying type.
请参阅Ecma International 中的ECMA 标准 335,公共语言基础设施 (CLI) 。CLI 允许底层类型为 char 或 bool,但 C# 和 VB.Net 不允许。就其价值而言,C++/CLI 确实允许 System::Char 作为基础类型。
I presume that C# and VB.Net don't allow char and bool as the underlying type for syntactical reasons only.
我认为 C# 和 VB.Net 仅出于语法原因不允许 char 和 bool 作为基础类型。
回答by Christoph
Targeting VB.NET and CLI consumers, you can compile the char enum like this (easiest with a VB.NET or C# project that has IL Support (an extension) enabled):
针对 VB.NET 和 CLI 使用者,您可以像这样编译 char 枚举(使用启用了 IL 支持(扩展)的 VB.NET 或 C# 项目最简单):
.class public auto ansi sealed Hafner.Testing.EdgeCase.CharEnum
extends [mscorlib]System.Enum
{
.field public specialname rtspecialname char value__
.field public static literal valuetype Hafner.Testing.EdgeCase.CharEnum Min = char(0)
.field public static literal valuetype Hafner.Testing.EdgeCase.CharEnum Zero = char(0)
.field public static literal valuetype Hafner.Testing.EdgeCase.CharEnum Max = char(65535)
.field public static literal valuetype Hafner.Testing.EdgeCase.CharEnum DuplicateSameValue = char(65) //A
.field public static literal valuetype Hafner.Testing.EdgeCase.CharEnum duplicateSameValue = char(65) //A
.field public static literal valuetype Hafner.Testing.EdgeCase.CharEnum DuplicateOtherValue = char(90) //Z
.field public static literal valuetype Hafner.Testing.EdgeCase.CharEnum duplicateOtherValue = char(88) //X
}