首页 > 其他分享 >枚举类型使用

枚举类型使用

时间:2023-09-24 23:01:57浏览次数:27  
标签:Console Description val System 枚举 WriteLine 使用 类型 using

1.首先我们建立一个枚举类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp3
{
    public enum Fruit
    {
        apple,
        peach,
        watermelon,
        banana,
        orange
    }
}

2.代码调用

using ConsoleApp3;
Console.WriteLine(Fruit.orange);
Console.WriteLine("Hello, World!");

3.当我们要使用中文的时候,这时候要进行变化,使用特性

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp3
{
    public enum Fruit
    {
        [Description("苹果")]
        apple,
        [Description("桃子")]
        peach,
        [Description("西瓜")]
        watermelon,
        [Description("香蕉")]
        banana,
        [Description("橘子")]
        orange
    }
    static class EnumExtensions
    {
        public static string GetDescription(this Enum val)
        {
            var field = val.GetType().GetField(val.ToString());
            if (field == null)
                return val.ToString();
            var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));     //使用反射,反射效率低
            return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description;
 
        }
    }
}

4.代码调用

using ConsoleApp3;
Console.WriteLine(Fruit.orange);
Console.WriteLine(Fruit.orange.GetDescription());
Console.WriteLine("Hello, World!");

5.如果我们有很多种类的水果的话,并且分类的话。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp3
{
    public enum Fruit
    {
        [levelOne("一级")]
        [Description("苹果")]
        apple,
        [levelOne("二级")]
        [Description("桃子")]
        peach,
        [Description("西瓜")]
        watermelon,
        [levelOne("三级")]
        [Description("香蕉")]
        banana,
        [levelOne("三级")]
        [Description("橘子")]
        orange
    }
    static class EnumExtensions
    {
        public static string GetDescription(this Enum val)
        {
            var field = val.GetType().GetField(val.ToString());
            if (field == null)
                return val.ToString();
            var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));     //使用反射,反射效率低
            return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description;
        }
        public static string Getlevel(this Enum val)
        {
            var field = val.GetType().GetField(val.ToString());
            if(field == null)
                return val.ToString();
            var customAttribute = Attribute.GetCustomAttribute(field, typeof(levelOne));
            return customAttribute == null ? val.ToString() : ((levelOne)customAttribute).Data;
        }
    }
    public class levelOne : Attribute
    {
        public string Data { get; set; }
        public levelOne() { }
        public levelOne(string data)
        {
            Data = data;
        }
    }
}

6.代码调用

using ConsoleApp3;
Console.WriteLine(Fruit.orange);
Console.WriteLine(Fruit.orange.GetDescription());
Console.WriteLine(Fruit.orange.Getlevel());
Console.WriteLine(Fruit.apple.Getlevel());
Console.WriteLine("Hello, World!");

标签:Console,Description,val,System,枚举,WriteLine,使用,类型,using
From: https://blog.51cto.com/u_15585624/7589433

相关文章

  • MySQL 的 blob 类型有哪些?
    MySQL中有两种主要的BLOB类型,用于存储二进制数据:TINYBLOB:TINYBLOB类型可以存储最多255字节的二进制数据。BLOB:BLOB类型可以存储最多65,535字节的二进制数据。此外,MySQL还有两种变体的BLOB类型,它们用于存储更大的二进制数据:MEDIUMBLOB:MEDIUMBLOB类型可以存储最多16,77......
  • MongoDB の 安装与基本使用
    安装mongo建议使用docker直接一键安装dockerrun--namemongo_zdp-p27017:27017-dmongo:latestGUI工具,我使用过的有两个,一个是navicate,一个是nosqlbooster。下载地址如下https://nosqlbooster.com/downloadsmysql和mogodb名称的对比mysqlMongoDB数据......
  • PostgreSQL教程:数值类型(整型、浮点型、序列、数值的常见操作)
    整型整型比较简单,主要就是三个:smallint、int2:2字节integer、int、int4:4字节bigint、int8:8字节正常没啥事就integer,如果要存主键,比如雪花算法,那就bigint。空间要节约,根据情况smallint浮点型浮点类型就关注2个(其实是一个)decimal(n,m):本质就是numeric,PGSQL会帮你转换numeric(n,m):PGSQL......
  • PostgreSQL教程:布尔类型
    布尔类型简单的丫批,可以存储三个值,true,false,null--布尔类型的约束没有那么强,true,false大小写随意,他会给你转,同时yes,no这种他也认识,但是需要转换selecttrue,false,'yes'::boolean,boolean'no',True,FaLse,NULL::boolean;boolean类型在做and和or的逻辑操作时,结果字段A字段Baand......
  • PostgreSQL教程:单引号和双引号的使用、数据类型转换
    单引号和双引号在PGSQL中,写SQL语句时,单引号用来标识实际的值。双引号用来标识一个关键字,比如表名,字段名。--单引号写具体的值,双引号类似MySQL的``标记,用来填充关键字--下面的葡萄牙会报错,因为葡萄牙不是关键字select1.414,'卡塔尔',"葡萄牙";数据类型转换第一种方式:只需要在值......
  • PostgreSQL教程:数据类型
    PGSQL支持的类型特别丰富,大多数的类型和MySQL都有对应的关系名称说明对比MySQL布尔类型boolean,标准的布尔类型,只能存储true,falseMySQL中虽然没有对应的boolean,但是有替换的类型,数值的tinyint类型,和PGSQL的boolean都是占1个字节。整型smallint(2字节),integer(4字节),bigint(8字节)跟MySQL没......
  • Angular ModuleWithProviders 类型的使用场景介绍
    import{ModuleWithProviders}from'@angular/core'这行代码在Angular中有着重要的作用,它引入了ModuleWithProviders类型从@angular/core模块中。为了更好地理解这行代码的作用,我们需要深入探讨Angular中模块(Modules)的概念以及如何使用ModuleWithProviders类型。Angula......
  • 使用 Docker Compose 安装 APISIX
    1.基本概念APISIX是Apache下的一款云原生的API网关,支持全生命周期的API管理,在应用中可以作为所有API调用的统一入口。APISIX有一些基础概念如下:Upstream,或者叫做上游,是一组目标主机的抽象,上游一组节点提供实际的业务服务,一般需要指定负载均衡策略去调用。Route,路由......
  • Angular APP_INITIALIZER Injection Token 的使用方法介绍
    import{APP_INITIALIZER}from'@angular/core'这行代码在Angular中的作用是导入名为APP_INITIALIZER的常量,它来自Angular核心模块@angular/core。APP_INITIALIZER是一个重要的Angular特性,它用于执行一系列初始化操作,通常用于配置应用程序之前执行一些必要的任务。......
  • 1787_函数指针的使用
    全部学习汇总:GitHub-GreyZhang/c_basic:littlebitsofc.前阵子似乎写了不少错代码,因为对函数指针的理解还不够。今天晚上似乎总算是梳理出了一点眉目,在先前自己写过的代码工程中做一下测试。先前实现过一个归并排序算法,算法函数的一个传入参数是指向一个比较功能函数的指针。......