c# 枚舉的使用

來源:互聯網
上載者:User

標籤:src   read   ida   使用   short   編譯器   資料   成功   注意   

  c#建立枚舉類型使用enum關鍵字,限制其值只能是一組符號名稱。

一、聲明枚舉

  定義枚舉要先寫一個enum關鍵字,後面跟上{},然後再{}內添加一組符號,這些符號標識了該枚舉類型可以擁有的合法值。例如:

 enum week { Monday, Tuesday, Wednesday, Thrusday, Friday, Saturday, Sunday }//聲明一個星期的枚舉

二、使用枚舉

  聲明好了之後,可以像使用其他任何類型一樣使用它們,上面聲明了一個week的枚舉下面我們使用它。例如

  

week weekday =  week.Monday;//這個位置相當於聲明一個局部變數,儲存Monday            int a = Convert.ToInt32(weekday);//a = week.Monday的int型資料            //try            //{            //    int a = Convert.ToInt32(weekday);            //    Console.WriteLine(a);            //}            //catch (System.FormatException)            //{            //    Console.WriteLine("格式錯誤");            //}              Console.WriteLine("{0}\t{1}",weekday,a);

  和所有的實值型別一樣,可以使用修飾符  ?  建立一個可空枚舉變數。這樣除了能把枚舉類型定義的賦值給這個變數,還可以吧null賦值給它。例如

 week weekday =  week.Monday;//這個位置相當於聲明一個局部變數,儲存Monday            week? weekdayTest = null;//使用?修飾weekdayTest變數 並賦值為null            Console.WriteLine(weekdayTest);//輸出null            Console.WriteLine(Convert .ToInt32(weekdayTest));//將null(weekdayTest)轉換為int型            weekdayTest = week.Sunday;//將weekdayTest賦值為week.Sunday            Console.WriteLine(weekdayTest);            int a = Convert.ToInt32(weekday);//a = week.Monday的int型資料            //try            //{            //    int a = Convert.ToInt32(weekday);            //    Console.WriteLine(a);            //}            //catch (System.FormatException)            //{            //    Console.WriteLine("格式錯誤");            //}              Console.WriteLine("{0}\t{1}",weekday,a);              Console.ReadLine();

 

 

  需要注意的是,使用Console.writeLine顯示枚舉變數時,編譯器會自動產生代碼,輸出和變數值匹配的字串,如果有必要,可以調用每個枚舉都有的ToString方法,顯示枚舉變數轉換成代表其當前值得字串。

三、選擇枚舉的字面值

   枚舉內部的每個元素都關聯(對應) 一個整數值。預設第一個元素對應整數0,以後每個元素都對應的整數都遞增1.資料可以從一種類型轉換為另一種類型,只要轉換結果是有效,有意義的都會轉換成功。

static void Main(string[] args)        {           week weekday =  week.Monday;//這個位置相當於聲明一個局部變數,儲存Monday            week? weekdayTest = null;//使用?修飾weekdayTest變數 並賦值為null            Console.WriteLine(weekdayTest);//輸出null            Console.WriteLine(Convert .ToInt32(weekdayTest));//將null(weekdayTest)轉換為int型            weekdayTest = week.Sunday;//將weekdayTest賦值為week.Sunday            Console.WriteLine(weekdayTest);            Console.WriteLine(Convert.ToInt32(weekdayTest));//將null(weekdayTest)轉換為int型            int a = Convert.ToInt32(weekday);//a = week.Monday的int型資料            //try            //{            //    int a = Convert.ToInt32(weekday);            //    Console.WriteLine(a);            //}            //catch (System.FormatException)            //{            //    Console.WriteLine("格式錯誤");            //}              Console.WriteLine("{0}\t{1}",weekday,a);              Console.ReadLine();           }

 

 

    注意:用於初始化枚舉字面值的整數值必須是編譯器編譯的時候能夠確定的常量值。

四、選擇枚舉的基礎類型

  在聲明枚舉的時候,枚舉字面值預設是int類型,但是可以讓枚舉類型基於不同的基礎整形類型。可以是:byte(Max:255),sbyte(Max:127),short,ushrot,int,uint,long,ulong。例如

 enum week:sbyte { Monday=1, Tuesday, Wednesday, Thrusday, Friday, Saturday, Sunday }//聲明一個星期的枚舉

 

c# 枚舉的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.