简体   繁体   English

C#将枚举转换为char

[英]C# Convert enum to char

So I have an enum like this: 所以我有一个这样的枚举:

enum element
{
    Side = '|', Top = '-', Corner = '.'
}

And then I have a two-dimensional char array which only contains the chars | 然后我有一个二维char数组,只包含chars | - . - 。

But when I try to write: 但是当我试着写:

array[2, 3] = element.Side; 

I get an error saying that "cannot implicitly convert type element to 'char'" 我得到一个错误,说“不能隐式地将类型元素转换为'char'”

So... What do I do wrong? 那么......我做错了什么? Is there an elegant way for me to turn my two-dimensional char array into an array containing the corresponding enums? 我是否有一种优雅的方式将我的二维char数组转换为包含相应枚举的数组? I hope you understand my question. 我希望你理解我的问题。

You have to cast the element.Side to a char: 你必须将element.Side转换为char:

arr[2, 3] = (char)element.Side

Why you should do this? 你为什么要这样做?

The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list. enum关键字用于声明枚举,这是一种由称为枚举器列表的命名常量组成的不同类型。

Every enumeration type has an underlying type, which can be any integral type except char . 每个枚举类型都有一个基础类型, 除了char之外它可以是任何整数类型 The default underlying type of enumeration elements is int. 枚举元素的默认基础类型是int。 To declare an enum of another integral type, such as byte, use a colon after the identifier followed by the type, as shown in the following example. 要声明另一个整数类型的枚举,例如byte,请在标识符后跟类型后使用冒号,如下例所示。

So what is happening by declaring your enum? 那么通过声明你的枚举会发生什么?

Under the cover, the compiler produces for each character of your enum it's correspoding integral representation. 在封面下,编译器为枚举的每个字符生成它的对应整数表示。 You could verify this, by creating a console application and trying to write to the console the following: 您可以通过创建控制台应用程序并尝试向控制台写入以下内容来验证这一点:

(int)element.Side

You will get 124. Please check here to verify that 124 is the decimal representation of vertical bar. 您将获得124.请在此处查看以验证124是竖线的十进制表示。

Furthermore, you could verify this by using a dissasembler. 此外,您可以使用dissasembler验证这一点。 The picture below has been taken using IL DASM. 下图是使用IL DASM拍摄的。

在此输入图像描述

If you click at the Side: public static literal... a new window would be opened with the following statement. 如果单击Side: public static literal...将使用以下语句打开一个新窗口。

在此输入图像描述

What is 0x0000007C? 什么是0x0000007C?

The Hexadecimal representation of vertical bar ! 垂直条的十六进制表示! You could find further info on this here . 你可以在这里找到更多相关信息。

You could find further info regarding enum here and here . 你可以在这里这里找到关于enum更多信息。

You cannot have char as your base type of an enum, therefore, it is not possible to implicitly convert an element to a char . 您不能将char作为枚举的基本类型,因此,无法将element隐式转换为char Although you have given char values to the elements in your enum, they are converted to their int values. 虽然您已为枚举中的元素指定了char值,但它们会转换为int值。 You have to cast before you use them: 你必须先使用它们进行施法:

arr[2, 3] = (char)element.Side

So actually, your code is: 实际上,您的代码是:

enum element : int
{
    Side = '|', Top = '-', Corner = '.'
}

And if you try to set your base type to a char , the compiler will complain. 如果您尝试将基类型设置为char ,编译器将会抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM