C#與C++枚舉的區(qū)別對比和使用案例
更新時間:2022年04月26日 08:27:58 作者:農(nóng)碼一生
本文詳細(xì)講解了C#與C++枚舉的區(qū)別對比和使用案例,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
C++與C#中枚舉的區(qū)別
一、C++
- 枚舉類型中的每個元素,可以直接使用,不必通過類型.元素的方式調(diào)用
- 沒有++操作
#include <iostream>
using namespace std;
enum week{Monday,Thuesday};
int main()
{
week day;
day = Monday;
day = Thuesday;
//day = 4; 報錯 類型轉(zhuǎn)化出錯
//day++; 出錯,沒有++ 操作
cout << day << endl;//輸出結(jié)果為1
return 0;
}二、C#
- 枚舉類型中的每個元素必須通過類型.元素的形式調(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ù)取得對應(yīng)的枚舉類型
NoticeType noticeType2 = (NoticeType)Char.Parse("A");//Notice
//通過名稱取得枚舉類型
NoticeType noticeType3 = (NoticeType)Enum.Parse(typeof(NoticeType), "Notice");
}二、獲取描述信息
[Description("會員等級")]
enum MemberLevel
{
[Description("金牌會員")]
gold = 1,
[Description("銀牌會員")]
silver = 2,
[Description("銅牌會員")]
copper = 3
}
/// <summary>
///
/// </summary>
/// <param name="value">枚舉值</param>
/// <param name="isTop">是否是頂級標(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ū)別對比和使用案例的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
協(xié)定需要會話,但是綁定“BasicHttpBinding”不支持它或者因配置不正確而無法支持它
在IIS7及以上版本服務(wù)器中提供了基于WAS的無.SVC文件的WCF服務(wù)激活功能,能夠提供基于HTTP和非HTTP協(xié)議的訪問,通過添加Windows Server AppFabric可以更方便的管理WCF服務(wù)2012-12-12
Unity技術(shù)手冊之Slider滑動器使用實例詳解
這篇文章主要為大家介紹了Unity技術(shù)手冊之Slider滑動器使用實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
C#構(gòu)造函數(shù)在基類和父類中的執(zhí)行順序
這篇文章介紹了C#構(gòu)造函數(shù)在基類和父類中的執(zhí)行順序,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
使用C#實現(xiàn)基于TCP和UDP協(xié)議的網(wǎng)絡(luò)通信程序的基本示例
這篇文章主要介紹了使用C#實現(xiàn)基于TCP和UDP協(xié)議的網(wǎng)絡(luò)通信程序的示例,文中分別編寫了基本的服務(wù)器端和客戶端,代碼十分簡單,需要的朋友可以參考下2016-04-04

