zoukankan      html  css  js  c++  java
  • C#枚举类型的使用示例

    介绍
    枚举是一个指定的常数,其基础类型可以是除 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");
  • 相关阅读:
    ASM:《X86汇编语言-从实模式到保护模式》1-4章:处理器,内存和硬盘基础
    Greedy:Paint Color(AOJ 0531)
    Match:DNA repair(POJ 3691)
    Match:Censored!(AC自动机+DP+高精度)(POJ 1625)
    Match:Keywords Search(AC自动机模板)(HDU 2222)
    BM算法和Sunday快速字符串匹配算法
    蓄水池抽样算法
    Match:Milking Grid(二维KMP算法)(POJ 2185)
    Match:Cyclic Nacklace(KMP的next数组的高级应用)(HDU 3746)
    KMP单模快速字符串匹配算法
  • 原文地址:https://www.cnblogs.com/plain/p/2017128.html
Copyright © 2011-2022 走看看