惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
T
Tenable Blog
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Securelist
P
Privacy & Cybersecurity Law Blog
Know Your Adversary
Know Your Adversary
T
The Exploit Database - CXSecurity.com
Latest news
Latest news
D
Darknet – Hacking Tools, Hacker News & Cyber Security
I
Intezer
F
Fortinet All Blogs
Engineering at Meta
Engineering at Meta
Simon Willison's Weblog
Simon Willison's Weblog
The Register - Security
The Register - Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
L
Lohrmann on Cybersecurity
C
Cyber Attacks, Cyber Crime and Cyber Security
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
H
Help Net Security
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
S
Schneier on Security
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
P
Privacy International News Feed
S
Secure Thoughts
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recorded Future
Recorded Future
C
Cybersecurity and Infrastructure Security Agency CISA
MyScale Blog
MyScale Blog
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
IT之家
IT之家
人人都是产品经理
人人都是产品经理
NISL@THU
NISL@THU
博客园 - Franky
T
Tor Project blog
G
GRAHAM CLULEY
博客园 - 【当耐特】
Jina AI
Jina AI
Security Archives - TechRepublic
Security Archives - TechRepublic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
A
About on SuperTechFans
Hacker News - Newest:
Hacker News - Newest: "LLM"

博客园 - 天际翔龙

一个苹果证书如何多次使用——导出p12文件[多台电脑使用] Log4net系列一:Log4net搭建之文本格式输出【转】 SMMS 2016 啟用深色主題 c#代碼小集 ZenCoding[Emmet]語法簡介【轉】 vscode: Visual Studio Code 常用快捷键【轉】 Android 杂记 解决genymotion-arm-translation.zip无法拖拽安装的问题[转] c#同步調用異步(async)方法【記錄用】 DDD基本概念 server2012/win8 卸载.net framework 4.5后 无法进入系统桌面故障解决【转】 Entity Framework中AutoDetectChangesEnabled為false時更新DB方法 git常用命令备忘录 MSSQL日誌傳輸熱備份注意事項 c#生成唯一编号方法记录,可用数据库主键 唯一+有序 Angular 隨記 使用dumpbin命令查看dll导出函数及重定向输出到文件【轉】 UML类图与类的关系详解【转】 知識隨記
C#枚举Enum[轉]
天际翔龙 · 2018-01-04 · via 博客园 - 天际翔龙

枚举是一个指定的常数,其基础类型可以是除 Char 外的任何整型。
如果没有显式声明基础类型,则使用 Int32。
编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举。
定义默认基数从O开始,也可指定数值。

範例一:

enum Days { Saturday=1, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };

Colors myColors = Colors.Red;
string strColor=myColors.tostring();
int    IntColor=(int)myColors ; 
//位或
Colors myColors = Colors.Red | Colors.Blue | Colors.Yellow;
//位与
Colors myColors = Colors.Red & Colors.Blue & Colors.Yellow;
//遍历 
foreach (string s in Enum.GetNames(typeof(Days)))
  Response.Write(s + "--" + Enum.Parse(typeof(Days), s).ToString());
//转换
Colors mc=Colors Enum.Parse(typeof(Colors ), "red"); 
 if (System.Enum.IsDefined(typeof(Days), "Monday"))
   Days ds= (Days)Enum.Parse(typeof(Days), "Monday");

範例二:

public enum NoticeType
{
        Notice = 'A',
        LabRule = 'H',
        HotInformation = 'N',
        Column = 'C',
        All = '1',
        Null = '0'
}
//新建枚举类型
NoticeType noticeType1 = NoticeType.Column;

//把枚举类型转换为string d="Column"
string d = noticeType1.ToString();

//取得枚举类型的基数 dd='C'
char dd = (char)noticeType1;

//通过基数取得对应的枚举类型 noticeType2 = NoticeType.Notice
//(NoticeType)'A';  两种方式都可以
NoticeType noticeType2 = (NoticeType)Char.Parse("A"); 

//通过名称取得枚举类型 noticeType3 = NoticeType.Notice
NoticeType noticeType3 = (NoticeType)Enum.Parse(typeof(NoticeType), "Notice");

获得文字描述:

using System.ComponentModel; // 先添加该引用
enum Direction
{
    [Description("this means facing to UP (Negtive Y)")]
    UP = 1,
    [Description("this means facing to RIGHT (Positive X)")]
    RIGHT = 2,
    [Description("this means facing to DOWN (Positive Y)")]
    DOWN = 3,
    [Description("this means facing to LEFT (Negtive X)")]
    LEFT = 4
};

//使用如下方法来获得文字描述:
using System.Reflection;
using System.ComponentModel;
public static String GetEnumDesc(Direction e)
{
    FieldInfo EnumInfo = e.GetType().GetField(e.ToString());
    DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[]) EnumInfo.
        GetCustomAttributes (typeof(DescriptionAttribute), false);
    if (EnumAttributes.Length > 0)
    {
        return EnumAttributes[0].Description;
    }
    return e.ToString();
}