前言:
在面對對象的動態開發中,難免會遇到各種類型轉換的問題,今天我們來簡單介紹一下enum(枚舉)與int(整型)和string(字符串)類型之間的相互轉換;
一.三者類型的簡單介紹:
1.enum
:枚舉的聲明用enum,枚舉是值類型;詳細介紹請前往:enum枚舉的介紹
2.int
:int
是32位有符號的整型;
int i =100 ;
Console.WriteLine(i);
3.string
:是字符串類型,雙引號中隨便寫,唯獨寫符號需要小心;
string i = "第一個字符串156456456=43567dufcdj";
Console.WriteLine(i);
二.enum枚舉轉換爲字符串類型:
在轉化爲字符串類型時主要用到enum_name.枚舉值.Tostring()
,也就是將當前對象轉換爲字符串樣式;
舉例:
enum Sss
{
喫飯,
睡覺,
打豆豆,
打遊戲,
看電影,
三國殺
}
static void Main(string[] args)
{
Console.WriteLine("這是將枚舉轉換爲字符串: {0}", Sss.打遊戲.ToString());
Console.ReadLine();
Console.WriteLine("這是將枚舉轉換爲字符串: {0}", Sss.三國殺.ToString());
Console.ReadLine();
Console.WriteLine("這是將枚舉轉換爲字符串: {0}", Sss.喫飯.ToString());
Console.ReadLine();
Console.WriteLine("這是將枚舉轉換爲字符串: {0}", Sss.打豆豆 .ToString());
Console.ReadLine();
}
運行結果:
三.enum枚舉轉化爲整型:
在轉換爲整形時一般用的是強制轉化,會用到(int)enum_name.枚舉值
,也就是將當前對象轉化爲整型;
舉例:
enum Sss
{
喫飯,
睡覺,
打豆豆,
打遊戲,
看電影,
三國殺
}
static void Main(string[] args)
{
Console.WriteLine("這是將枚舉轉換爲整型: {0}", (int)Sss.喫飯);
Console.ReadLine();
Console.WriteLine("這是將枚舉轉換爲整型: {0}", (int)Sss.三國殺);
Console.ReadLine();
Console.WriteLine("這是將枚舉轉換爲整型: {0}", (int)Sss.打遊戲);
Console.ReadLine();
Console.WriteLine("這是將枚舉轉換爲整型: {0}", (int)Sss.打豆豆);
Console.ReadLine();
Console.WriteLine("這是將枚舉轉換爲整型: {0}", (int)Sss.看電影);
Console.ReadLine();
}
運行結果:
如果好奇怎麼回得到這樣的結果,請細閱:enum枚舉詳解
如果好奇怎麼不用int.Parse()
,那是由於這個方法是將其數字的字符串形式轉化爲與其等效的32位有符號整數,如果引用這種會報錯,有兩種報錯形式,一種在編寫時報錯,一種在運行時報錯;
一.編寫時報錯:
二.編寫時正確,運行時報錯;
所以一般推薦用(int)enum_name.枚舉值
這種書寫!
四.將整型轉化爲enum枚舉:
1.第一種方法
在將整型轉化爲枚舉時主要用到了:(enum_name)enum符號的值
這個方法;
enum Sss
{
喫飯,
睡覺,
打豆豆,
打遊戲,
看電影,
三國殺
}
static void Main(string[] args)
{
Console.WriteLine("這是將整數轉換爲枚舉: {0}", (Sss)1);
Console.ReadLine();
Console.WriteLine("這是將整數轉換爲枚舉: {0}", (Sss)2);
Console.ReadLine();
Console.WriteLine("這是將整數轉換爲枚舉: {0}", (Sss)3);
Console.ReadLine();
Console.WriteLine("這是將整數轉換爲枚舉: {0}", (Sss)4);
Console.ReadLine();
Console.WriteLine("這是將整數轉換爲枚舉: {0}", (Sss)5);
Console.ReadLine();
}
代碼中的1,2,3,4,5爲枚舉符號值,所以運行結果爲:
2.第二種方法:
在這種方法中主要用到了:Enum.GetName(typeof(enum_name), enum符號值)
這個方法,Enum.GetName()
的意思就是:在制定的枚舉中檢索具有指定值的常熟的名稱,括號裏寫的是枚舉的名稱以及枚舉的符號值;
舉例;
enum Sss
{
喫飯,
睡覺,
打豆豆,
打遊戲,
看電影,
三國殺
}
static void Main(string[] args)
{
Console.WriteLine("這是將整數轉換爲枚舉: {0}", Enum.GetName(typeof(Sss), 1));
Console.ReadLine();
Console.WriteLine("這是將整數轉換爲枚舉: {0}", Enum.GetName(typeof(Sss), 2));
Console.ReadLine();
Console.WriteLine("這是將整數轉換爲枚舉: {0}", Enum.GetName(typeof(Sss), 3));
Console.ReadLine();
Console.WriteLine("這是將整數轉換爲枚舉: {0}", Enum.GetName(typeof(Sss), 4));
Console.ReadLine();
Console.WriteLine("這是將整數轉換爲枚舉: {0}", Enum.GetName(typeof(Sss), 5));
Console.ReadLine();
}
和第一種方法一樣,所以結果不想而知了吧:
但個人一般推薦第一種因爲比較簡單,哈哈哈
五.將字符串轉化爲enum枚舉:
在其進行轉化是首先肯定得定義一個string
類型的量,然後通過(enum_name)Enum.Parse(typeof(enum_name), string_name)
方法進行設置調用;
舉例:
enum Sss
{
cc,
dd,
bb,
打遊戲,
看電影,
三國殺
}
static void Main(string[] args)
{
string str = "11223344";
//Sss sss = (Sss)Enum.Parse(typeof(Sss),str);
Console.WriteLine("這是將字符串轉換爲枚舉: {0}", (Sss)Enum.Parse(typeof(Sss), str));
Console.ReadLine();
}
運行結果:
以上爲本次博客全部內容,下次更新如何查看枚舉具體轉換的是什麼類型,敬請期待!