本篇内容介绍了“C#与C++枚举的区别是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
枚举类型中的每个元素,可以直接使用,不必通过类型.元素的方式调用
没有++操作
#include <iostream>
using namespace std;
enum week{Monday,Thuesday};
int main()
{
week day;
day = Monday;
day = Thuesday;
//day = 4; 报错 类型转化出错
//day++; 出错,没有++ 操作
cout << day << endl;//输出结果为1
return 0;
}
枚举类型中的每个元素必须通过类型.元素的形式调用
可以++操作
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
}
}
}
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;
//把枚举类型转换为string d="Column"
string d = noticeType1.ToString();
//取得枚举类型的基数 'C'
char dd = (char)noticeType1;
//通过基数取得对应的枚举类型
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">是否是顶级标题的描述信息</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
{
// 获取枚举常数名称。
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;
}
调用
MemberLevel gold = MemberLevel.gold;
Console.WriteLine(gold.GetDescription());
System.Console.Read();
“C#与C++枚举的区别是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。