.NET Enum基本类型的Char

时间:2021-11-08 17:09:31

According to msdn Enums cannot have a base type of char. Why can enums not have a base type of char? Also why does microsoft strongly recommend that an enum contain a constant with a value of 0? Thank you very much

根据msdn Enums不能有基本类型的char。为什么枚举不能有基本类型的char?另外为什么微软强烈建议枚举包含一个值为0的常量?非常感谢你

4 个解决方案

#1


5  

You can find a link to the C# language specification. I think the reason for the restriction probably derives from these statements in the language specification, section 4.1.5.

您可以找到C#语言规范的链接。我认为限制的原因可能源于语言规范第4.1.5节中的这些陈述。

The char type is classified as an integral type, but it differs from the other integral types in two ways:

char类型被归类为整数类型,但它在两个方面与其他整数类型不同:

  • 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.

    其他类型没有隐式转换为char类型。特别是,即使sbyte,byte和ushort类型具有使用char类型可完全表示的值范围,也不存在从sbyte,byte或ushort到char的隐式转换。

  • 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类型。例如,(char)10与'\ x000A'相同。

As far as why zero, because the default value of an uninitialized enum is 0.

至于为什么为零,因为未初始化的枚举的默认值为0。

#2


3  

Although as you've seen here, you are specifically not allowed to define a character enum because there is no implicit conversion from char to any other integral type, there is an integral conversion from char to integer, so you can do this:

虽然正如你在这里看到的,你特别不允许定义一个字符枚举,因为没有从char到任何其他整数类型的隐式转换,有一个从char到整数的整数转换,所以你可以这样做:

public enum MyCharEnum
{
    BEL = 0x07,
    CR = 0x0D,
    A = 'A',
    z = 'z'
}

And use an explicit conversion to char, thus:

并使用显式转换为char,因此:

MyCharEnum thing = MyCharEnum.BEL;
char ch = (char)thing;
System.Console.WriteLine(ch.ToString());

(and, yes, it makes a beep - just like the good old days!)

(是的,它会发出哔哔声 - 就像过去的美好时光!)

#3


2  

Why can enums not have a base type of char?

为什么枚举不能有基本类型的char?

Because it doesn't really make sense (in much the same way that having a base type of float doesn't make sense). Under the covers an enum is an integer, however as a char can be any unicode character there isn't really a stuiable mapping from chars to integers.

因为它确实没有意义(与基本类型的浮点没有意义的方式大致相同)。在封面下,枚举是一个整数,但是因为char可以是任何unicode字符,所以实际上没有从chars到整数的可靠映射。

Why does microsoft strongly recommend that an enum contain a constant with a value of 0?

为什么microsoft强烈建议枚举包含值为0的常量?

Because the default value of an un-initialised enum is zero (see Enums should have zero value)

因为未初始化的枚举的默认值为零(请参阅枚举应该为零值)

#4


-1  

Why can enums not have a base type of char?

为什么枚举不能有基本类型的char?

I would suggest that this is due to the way that .NET handles Chars as unicode meaning that a Char != Byte (it's value can be greater than 255). Enum's cannot be based on a Boolean type either and that is also an integral type. Also using a Char as the base type wouyld duplicate an existing base type so there is no point in doing so.

我建议这是由于.NET将Chars作为unicode处理的方式,这意味着Char!= Byte(它的值可以大于255)。枚举也不能基于布尔类型,也是一个整数类型。同样使用Char作为基类型wouyld复制现有的基类型,因此没有必要这样做。

Why does microsoft strongly recommend that an enum contain a constant with a value of 0?

为什么microsoft强烈建议枚举包含值为0的常量?

The reason why you are recommended to always have a value equal to 0 is because an enum is a value type and when a new one is made it will have the value of 0.

建议您始终具有等于0的值的原因是因为枚举是值类型,而当创建新值时,它将具有值0。

#1


5  

You can find a link to the C# language specification. I think the reason for the restriction probably derives from these statements in the language specification, section 4.1.5.

您可以找到C#语言规范的链接。我认为限制的原因可能源于语言规范第4.1.5节中的这些陈述。

The char type is classified as an integral type, but it differs from the other integral types in two ways:

char类型被归类为整数类型,但它在两个方面与其他整数类型不同:

  • 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.

    其他类型没有隐式转换为char类型。特别是,即使sbyte,byte和ushort类型具有使用char类型可完全表示的值范围,也不存在从sbyte,byte或ushort到char的隐式转换。

  • 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类型。例如,(char)10与'\ x000A'相同。

As far as why zero, because the default value of an uninitialized enum is 0.

至于为什么为零,因为未初始化的枚举的默认值为0。

#2


3  

Although as you've seen here, you are specifically not allowed to define a character enum because there is no implicit conversion from char to any other integral type, there is an integral conversion from char to integer, so you can do this:

虽然正如你在这里看到的,你特别不允许定义一个字符枚举,因为没有从char到任何其他整数类型的隐式转换,有一个从char到整数的整数转换,所以你可以这样做:

public enum MyCharEnum
{
    BEL = 0x07,
    CR = 0x0D,
    A = 'A',
    z = 'z'
}

And use an explicit conversion to char, thus:

并使用显式转换为char,因此:

MyCharEnum thing = MyCharEnum.BEL;
char ch = (char)thing;
System.Console.WriteLine(ch.ToString());

(and, yes, it makes a beep - just like the good old days!)

(是的,它会发出哔哔声 - 就像过去的美好时光!)

#3


2  

Why can enums not have a base type of char?

为什么枚举不能有基本类型的char?

Because it doesn't really make sense (in much the same way that having a base type of float doesn't make sense). Under the covers an enum is an integer, however as a char can be any unicode character there isn't really a stuiable mapping from chars to integers.

因为它确实没有意义(与基本类型的浮点没有意义的方式大致相同)。在封面下,枚举是一个整数,但是因为char可以是任何unicode字符,所以实际上没有从chars到整数的可靠映射。

Why does microsoft strongly recommend that an enum contain a constant with a value of 0?

为什么microsoft强烈建议枚举包含值为0的常量?

Because the default value of an un-initialised enum is zero (see Enums should have zero value)

因为未初始化的枚举的默认值为零(请参阅枚举应该为零值)

#4


-1  

Why can enums not have a base type of char?

为什么枚举不能有基本类型的char?

I would suggest that this is due to the way that .NET handles Chars as unicode meaning that a Char != Byte (it's value can be greater than 255). Enum's cannot be based on a Boolean type either and that is also an integral type. Also using a Char as the base type wouyld duplicate an existing base type so there is no point in doing so.

我建议这是由于.NET将Chars作为unicode处理的方式,这意味着Char!= Byte(它的值可以大于255)。枚举也不能基于布尔类型,也是一个整数类型。同样使用Char作为基类型wouyld复制现有的基类型,因此没有必要这样做。

Why does microsoft strongly recommend that an enum contain a constant with a value of 0?

为什么microsoft强烈建议枚举包含值为0的常量?

The reason why you are recommended to always have a value equal to 0 is because an enum is a value type and when a new one is made it will have the value of 0.

建议您始终具有等于0的值的原因是因为枚举是值类型,而当创建新值时,它将具有值0。