Summary of C#'s conversion between enumeration (enum) types, integers, and strings

Summary of C#'s conversion between enumeration (enum) types, integers, and strings

First, declare a variable of enum type:

//除枚举类型转换成整数类型示范中修改了该代码,其他均采用此枚举类型的声明。
[public] enum Subject
{
    
    
    Chinese,
    Math,
    English,
    AndroidDevelop,
    GameDevelop
}

Note:
① [public] keyword depends on the situation and can be omitted.
②It is best to define the enumeration directly under the namespace so that all classes in the namespace can use it.
③The last value can be added【,】or not.

Four types of conversion methods:

1. Enumeration (enum) type → integer (int) type

Subject subject1 = Subject.Chinese;
Subject subject2 = Subject.AndroidDevelop;
int num1 = (int)subject1;
int num2 = (int)subject2;
Console.WriteLine(num1);//输出:0
Console.WriteLine(num2);//输出:3
Console.ReadKey();
注:第一个枚举元素的值为0,后面每个枚举元素的值依次递增。

After modifying the value of the enumeration element, check the change rule of the value of the enumerator. It is not difficult to find that the enumeration element sequence can be forcibly modified to increment from a certain value.

public enum Subject
{
    
    
	//仅仅在第1小点这里修改枚举类型元素的序号
    Chinese = 5,
    Math,
    English = 10,
    AndroidDevelop,
    GameDevelop
}
Console.WriteLine((int)Subject.Chinese);//输出:5
Console.WriteLine((int)Subject.Math);//输出:6
Console.WriteLine((int)Subject.English);//输出:10
Console.WriteLine((int)Subject.AndroidDevelop);//输出:11
Console.WriteLine((int)Subject.GameDevelop);//输出:12
Console.ReadKey();

2. Integer (int) type → enumeration (enum) type

int i = 3;
Subject subject = (Subject)i;
Console.WriteLine(subject);//输出:AndroidDevelop
Console.ReadKey();

It can be seen that in the process of converting to an enumeration type, when the element number of the enumeration type includes the value of the integer type, the element of the enumeration type is obtained. If the value of the integer type is not included, the value itself is obtained.

int i = 8;
Subject subject = (Subject)i;
Console.WriteLine(subject);//输出:8
Console.ReadKey();

3. Enumeration (enum) type→string (string) type

Subject subject = Subject.GameDevelop;
string str = subject.ToString();
Console.WriteLine(str);//输出:GameDevelop
Console.ReadKey();

Everything can be ToString():

int i = 1;
double d = 2.2;
char c = 'C';
decimal m = 200.5m;
Console.WriteLine(i.ToString());//输出:1
Console.WriteLine(d.ToString());//输出:2.2
Console.WriteLine(c.ToString());//输出:C
Console.WriteLine(m.ToString());//输出:200.5
Console.ReadKey();

4. String (string) type → enumeration (enum) type

① The value of the string content is consistent with the name of the enumeration element, and the name of the enumeration element is output.

string str1 = "Math";
Subject subject1 = (Subject)Enum.Parse(typeof(Subject), str1);
Console.WriteLine(subject1);//输出:Math
Console.ReadKey();

②The value of the string content is consistent with the serial number of the enumeration element , and the enumeration element name of the corresponding serial number value is output.

string str2 = "3";
Subject subject2 = (Subject)Enum.Parse(typeof(Subject), str2);
Console.WriteLine(subject2);//输出:AndroidDevelop
Console.ReadKey();

③The value of the string content is inconsistent with the serial number of the enumeration element , and the value of the string content is output.

string str3 = "10";
Subject subject3 = (Subject)Enum.Parse(typeof(Subject), str3);
Console.WriteLine(subject3);//输出:10
Console.ReadKey();

④Except for the value and the text content consistent with the enumeration elements, the text content of other strings cannot be converted into the enumeration type, so an exception will be thrown.

//这里是错误示范!代码存在异常!
string str3 = "异常内容";
Subject subject3 = (Subject)Enum.Parse(typeof(Subject), str3);//抛出异常
Console.WriteLine(subject3);
Console.ReadKey();

Due to the limited energy of the author, some mistakes and omissions will inevitably appear in the article. Experts and netizens are welcome to criticize and correct me.

Guess you like

Origin blog.csdn.net/qq_46051312/article/details/123078574