C# 中的 enum(枚举) 类型使用例子

一、需要根据数字获取中文名称,C# 代码里面出现if 或switch 判断语句,比如下面的类为test1.class

          //获取计算类型的值
           string AggregateType = string.Empty;//中文名称
           int aggregateTypeint = 获取计算类型的值;
           switch (aggregateTypeint)
            {
                case 300000000:
                    AggregateType = "count";
                    break;
                case 300000001:
                    AggregateType = "sum";
                    break;
                case 300000002:
                    AggregateType = "max";
                    break;
                case 300000003:
                    AggregateType = "min";
                    break;
                case 300000004:
                    AggregateType = "avg";
                    break;
            }

那么可以新建一个类test2.class 如下:

   /// <summary>
    /// 聚合运算类型
    /// </summary>
    public enum AggregationType
    {
        /// <summary>
        /// 计数
        /// </summary>
        count = 300000000,
        /// <summary>
        /// 合计
        /// </summary>
        sum = 300000001,
        /// <summary>
        /// 最大值
        /// </summary>
        max = 300000002,
        /// <summary>
        /// 最小值
        /// </summary>
        min = 300000003,
        /// <summary>
        /// 平均值
        /// </summary>
        avg = 300000004
    }

那么:test1.class 可以改为:

            //获取计算类型的值
            string AggregateType = string.Empty;//中文名称
            int aggregateTypeint = 获取计算类型的值;
            AggregateType = ((AggregationType)aggregateTypeint).ToString();
原文地址:https://www.cnblogs.com/allenhua/p/3290607.html