C#與C++枚舉的區(qū)別對(duì)比和使用案例
C++與C#中枚舉的區(qū)別
一、C++
- 枚舉類型中的每個(gè)元素,可以直接使用,不必通過類型.元素的方式調(diào)用
- 沒有++操作
#include <iostream> using namespace std; enum week{Monday,Thuesday}; int main() { week day; day = Monday; day = Thuesday; //day = 4; 報(bào)錯(cuò) 類型轉(zhuǎn)化出錯(cuò) //day++; 出錯(cuò),沒有++ 操作 cout << day << endl;//輸出結(jié)果為1 return 0; }
二、C#
- 枚舉類型中的每個(gè)元素必須通過類型.元素的形式調(diào)用
- 可以++操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace myEnum_Structure { enum Week { Monday, Thuesday, Wednesday, Thursday, Friday, Saturday, Sunday } class Program { static void Main(string[] args) { Week day; day = Week.Sunday; Console.WriteLine(day);//輸出Sunday day++; Console.WriteLine(day);//輸出7 } } }
C#枚舉案例
一、普通調(diào)用
public enum NoticeType { Notice = 'A', LabRule = 'H', HotInformation = 'N', Column = 'C', All = '1', Null = '0' } private void button1_Click(object sender, EventArgs e) { //新建枚舉類型 NoticeType noticeType1 = NoticeType.Column; //把枚舉類型轉(zhuǎn)換為string d="Column" string d = noticeType1.ToString(); //取得枚舉類型的基數(shù) 'C' char dd = (char)noticeType1; //通過基數(shù)取得對(duì)應(yīng)的枚舉類型 NoticeType noticeType2 = (NoticeType)Char.Parse("A");//Notice //通過名稱取得枚舉類型 NoticeType noticeType3 = (NoticeType)Enum.Parse(typeof(NoticeType), "Notice"); }
二、獲取描述信息
[Description("會(huì)員等級(jí)")] enum MemberLevel { [Description("金牌會(huì)員")] gold = 1, [Description("銀牌會(huì)員")] silver = 2, [Description("銅牌會(huì)員")] copper = 3 } /// <summary> /// /// </summary> /// <param name="value">枚舉值</param> /// <param name="isTop">是否是頂級(jí)標(biāo)題的描述信息</param> /// <returns></returns> public static string GetDescription(this Enum value, bool isTop = false) { Type enumType = value.GetType(); DescriptionAttribute attr = null; if (isTop) { attr = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute)); } else { // 獲取枚舉常數(shù)名稱。 string name = Enum.GetName(enumType, value); if (name != null) { // 獲取枚舉字段。 FieldInfo fieldInfo = enumType.GetField(name); if (fieldInfo != null) { // 獲取描述的屬性。 attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute; } } } if (attr != null && !string.IsNullOrEmpty(attr.Description)) return attr.Description; else return string.Empty; }
調(diào)用
MemberLevel gold = MemberLevel.gold; Console.WriteLine(gold.GetDescription()); System.Console.Read();
到此這篇關(guān)于C#與C++枚舉的區(qū)別對(duì)比和使用案例的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
協(xié)定需要會(huì)話,但是綁定“BasicHttpBinding”不支持它或者因配置不正確而無法支持它
在IIS7及以上版本服務(wù)器中提供了基于WAS的無.SVC文件的WCF服務(wù)激活功能,能夠提供基于HTTP和非HTTP協(xié)議的訪問,通過添加Windows Server AppFabric可以更方便的管理WCF服務(wù)2012-12-12Unity技術(shù)手冊(cè)之Slider滑動(dòng)器使用實(shí)例詳解
這篇文章主要為大家介紹了Unity技術(shù)手冊(cè)之Slider滑動(dòng)器使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11C#構(gòu)造函數(shù)在基類和父類中的執(zhí)行順序
這篇文章介紹了C#構(gòu)造函數(shù)在基類和父類中的執(zhí)行順序,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04C#實(shí)現(xiàn)六大設(shè)計(jì)原則之迪米特法則
這篇文章介紹了C#實(shí)現(xiàn)六大設(shè)計(jì)原則之迪米特法則的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02使用C#實(shí)現(xiàn)基于TCP和UDP協(xié)議的網(wǎng)絡(luò)通信程序的基本示例
這篇文章主要介紹了使用C#實(shí)現(xiàn)基于TCP和UDP協(xié)議的網(wǎng)絡(luò)通信程序的示例,文中分別編寫了基本的服務(wù)器端和客戶端,代碼十分簡單,需要的朋友可以參考下2016-04-04C# 啟動(dòng) SQL Server 服務(wù)的實(shí)例
下面小編就為大家分享一篇C# 啟動(dòng) SQL Server 服務(wù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12C# Winform實(shí)現(xiàn)自定義分頁控件
一些第三方的分頁控件要么就是界面不夠美觀大方,要么就是使用起來感覺很麻煩,所以本文就為大家介紹一下如何利用Winform自定義分頁控件,需要的可以參考一下2023-07-07C#基于數(shù)據(jù)庫存儲(chǔ)過程的AJAX分頁實(shí)例
這篇文章主要介紹了C#基于數(shù)據(jù)庫存儲(chǔ)過程的AJAX分頁實(shí)現(xiàn)方法,以實(shí)例形式詳細(xì)講述了數(shù)據(jù)庫存儲(chǔ)過程的定義、數(shù)據(jù)庫的訪問及Ajax的實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-01-01