C# enumeration uses sorting_C# enum detailed explanation

1. Definition of enumeration

An enumerated type is a value type defined by a set of named constants of an underlying integral numeric type .

The System.Enum type is the abstract base class for all enumerated types. It provides several methods to obtain information about the enumeration type and its values. See the System.Enum API reference page for more information and examples.

You can use System.Enum in a base class constraint (called an enumeration constraint) to specify that the type parameter is the enumeration type. All enumerated types also satisfy the struct constraint, which specifies that a type parameter is a non-nullable value type .

Advantages of using enums:

1. It increases the readability of the code and makes the code more clear; it allows the use of descriptive names for integer values.

2. Standardize business classification, business type; help to ensure that variables are assigned legal and expected values.

scenes to be used:

Data classification, operation type, order status/process status

Second, the use of enumeration

By default, the associated constant value of an enumeration member is of type int;

They start at zero and increment by 1 in the order of the definition text.

Any other integer numeric type can be explicitly specified as the underlying type of the enumerated type.

You can also explicitly specify associated constant values. keyword enum

enum Season
{
    Spring,//标识0
    Summer,//标识1
    Autumn,//标识2
    Winter //标识3
}

specified number

enum ErrorCode : ushort
{
    None = 0,
    Unknown = 1,
    ConnectionLost = 100,
    OutlierReading = 200
}

3. Conversion of enumeration and numbers

Data type conversion:

//将枚举,转换成数字
int num = Season.春天.GetHashCode();
Console.WriteLine(num);

//将数字,转换成枚举
Season seson = (Season)System.Enum.ToObject(typeof(Season), 1);

Common operation packages:

        /// <summary>
        /// 获取指定类型的int值得枚举对象
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="t">类型</param>
        /// <returns></returns>
        public static T ToObject<T>(int value)
        {
            Type t = typeof(T);
            return (T)System.Enum.ToObject(t, value);
        }
        /// <summary>
        /// 指定枚举的string值,获取枚举对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T Parse<T>(string value)
        {
            return (T)System.Enum.Parse(typeof(T), value);
        }
        /// <summary>
        /// 获取指定类型的枚举列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Dictionary<int, string> GetList<T>()
        {
            Type t = typeof(T);
            Dictionary<int, string> dic = new Dictionary<int, string>();
            foreach (var item in System.Enum.GetValues(t))
            {
                dic.Add((int)item, System.Enum.GetName(t, item));
            }
            return dic;
        }
        // 获取name
        public static string GetName<T>(int id)
        {
            Type t = typeof(T);
            return System.Enum.ToObject(t, id).ToString();
        }
        public static List<object> GetNameList<T>()
        {
            Type t = typeof(T);
            List<object> res = new List<object>();
            foreach (var item in System.Enum.GetValues(t))
            {
                res.Add(new { id = (int)item, name = System.Enum.GetName(t, item) });
            }
            return res;
        }

More:

C# removes spaces in strings and organizes them

C# array grouping_C# data grouping_C# Linq grouping use sorting

C# time use sorting, DateTime use sorting

Guess you like

Origin blog.csdn.net/u011127019/article/details/131740128