首页 > 编程语言 >C#枚举 Enum

C#枚举 Enum

时间:2022-11-25 17:44:11浏览次数:44  
标签:Friday enum Console C# Enum WeekDays 枚举 WriteLine

 枚举是直接在命名空间、类或结构中使用 enum 关键字定义的。所有常量名都可以在大括号内声明,并用逗号分隔。

 枚举可以是任何数字数据类型,例如 byte,sbyte,short,ushort,int,uint,long 或 ulong。但是,枚举不能为字符串类型。

  1. 枚举(Enum)定义
           public enum WeekDays
            {
                Monday = 0,
                Tuesday = 1,
                Wednesday = 2,
                Thursday = 3,
                Friday = 4,
                Saturday = 5,
                Sunday = 6
            }

     

  2. 枚举值获取
           Console.WriteLine(Enum.GetName(typeof(WeekDays), 4));//输出:Friday
           Console.WriteLine(WeekDays.Friday);//输出:Monday
           Console.WriteLine((int) WeekDays.Friday);//输出:4
    
           foreach (var item in Enum.GetValues(typeof(WeekDays)))
           {
               Console.WriteLine((int) item);//循环输出:0,1...
           }
           foreach (var item in Enum.GetNames(typeof(WeekDays)))
           {
               Console.WriteLine(item);//循环输出:Monday,Tuesday...
           }

     

  3. Description、EnumMember的使用,完整实列
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study
    {
        public class MyEnum
        {
            public enum WeekDays
            {
                [EnumMember(Value = "周一,还有四天.... ")]
                Monday = 0,
                [Description("周二,还有三天....")]
                Tuesday = 1,
                Wednesday = 2,
                Thursday = 3,
                Friday = 4,
                Saturday = 5,
                Sunday = 6
            }
            enum MemberLevel : long
            {
                gold = 2147483648L,
                silver = 232L,
                copper = 10L
            }
    
            public MyEnum()
            {
                Employ(WeekDays.Friday);
            }
            public static void Employ(WeekDays weekDays)
            {
                switch (weekDays)
                {
                    case WeekDays.Monday:
                        break;
                    case WeekDays.Tuesday:
                        break;
                    case WeekDays.Wednesday:
                        break;
                    case WeekDays.Thursday:
                        break;
                    case WeekDays.Friday:
                        Console.WriteLine(Enum.GetName(typeof(WeekDays), 4));//输出:Friday
                        Console.WriteLine(WeekDays.Friday);//输出:Monday
                        Console.WriteLine((int)WeekDays.Friday);//输出:4
                        foreach (var item in Enum.GetValues(typeof(WeekDays)))
                        {
                            Console.WriteLine((int)item);//循环输出:0,1...
                        }
                        foreach (var item in Enum.GetNames(typeof(WeekDays)))
                        {
                            Console.WriteLine(item);//循环输出:Monday,Tuesday...
                        }
                        Console.WriteLine(WeekDays.Monday.ToEnumMemberAttrValue());//输出:周一,还有四天....
                        Console.WriteLine(WeekDays.Tuesday.GetDescription());//输出:周二,还有三天....
                        break;
                    case WeekDays.Saturday:
                        break;
                    case WeekDays.Sunday:
                        break;
                    default:
                        break;
                }
            }
        }
        public static class EnumExt
        {
            public static string ToEnumMemberAttrValue(this Enum @enum)
            {
                var attr =
                    @enum.GetType().GetMember(@enum.ToString()).FirstOrDefault()?.
                        GetCustomAttributes(false).OfType<EnumMemberAttribute>().
                        FirstOrDefault();
                if (attr == null)
                    return @enum.ToString();
                return attr.Value;
    
            }
    
            public static string GetDescription(this Enum @enum, bool isTop = false)
            {
                Type enumType = @enum.GetType();
                DescriptionAttribute attr = null;
                if (isTop)
                {
                    attr = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute));
                }
                else
                {
                    // 获取枚举常数名称。
                    string name = Enum.GetName(enumType, @enum);
                    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;
            }
        }
    }

     

标签:Friday,enum,Console,C#,Enum,WeekDays,枚举,WriteLine
From: https://www.cnblogs.com/Aatao/p/16925578.html

相关文章

  • OCC 细分TopoDS_Edge
    //任意EdgeTopoDS_Edgeedge;BRepAdaptor_CurvecurveAdaptor(edge);//方法1doublestart=curveAdaptor.FirstParameter();doubleend=curveAdaptor.LastParam......
  • 精通正则表达式- JavaScript的实现和应用
    1.正则表达式对象模式/pattern/flagspattern是任何简单或复杂的正则表达式,可以包含字符类,限定符,分组,向前查找以及反向引用。flags是匹配模式标明正则表达式的......
  • SpringCloud Alibaba(一) - Nacos 服务注册与发现,OpenFeign远程调用
    1、基础项目过目介绍1.1数据库创建1.2项目模块分布1.3测试http接口调用1.3.1http接口调用配置类//http接口调用配置类@ConfigurationpublicclassRestTempla......
  • 【Azure Fabric Service】Service Fabric 托管群集通过 Connect-ServiceFabricCluster
    问题描述ServiceFabric托管群集,使用KeyVault中证书,把证书导入到本地安装后,使用该证书的Thumbprint作为指令 Connect-ServiceFabricCluster 的 ServerCertThumb......
  • JavaScript 基础
    JavaScript使用JS的几种写法行内式的js直接写道元素的内部​​<buttontype="button"value="须弥"onclick="alert('纳西妲')"></button>​​内嵌式js引入式​​<scripts......
  • docker搭建Elasticsearch、Kibana、Logstash 同步mysql数据到ES
    一、前言在数据量大的企业级实践中,Elasticsearch显得非常常见,特别是数据表超过千万级后,无论怎么优化,还是有点力不从心!使用中,最首先的问题就是怎么把千万级数据同步到Elasti......
  • 浅谈TCP三次握手
    前言TCP三次握手和四次挥手是面试题的热门考点,它们分别对应TCP的连接和释放过程,今天先来认识一下TCP三次握手过程,以及是否可以使用“两报文握手”建立连接?1、TCP是什么T......
  • 一、css简介
    网页分成三部分结构(html)表现(css)行为(javascript)css​ -层叠样式表​ -网页实际上是一个多层的结构通过css可以分别为网页的每一层来设置样式,而最终我们能看到只是......
  • IDEA插件List of plugins was not loaded for following hosts: http://plugins.jetbr
    IDEA插件市场点开资源库后报错:Listofpluginswasnotloadedforfollowinghosts:http://plugins.jetbrains.com。解决办法:File-Settings-Plugins-Browserepositori......
  • 二、css语法
    style标签css中的注释/*这是注释*/css的基本语法:选择器声明块选择器,通过选择器可以选中页面中的指定元素,比如p的作用就是选中页面中的所有的p元素​ 声明块,通过......