定义枚举类型带有byte 的作用

51 篇文章 1 订阅

定义枚举类型带有byte 的作用

public enum Icon_Type : byte
    {
        /// <summary>
        /// 操作成功
        /// </summary>
        Ok,
        /// <summary>
        /// 操作标示
        /// </summary>
        Alert,
        /// <summary>
        /// 操作失败
        /// </summary>
        Error
    }

继承: byte,这个表示枚举元素使用 byte 存储。

枚举类型默认时:public enum Icon_Type : int

byte 是 0~255之间的整数,int 是-2147483648~2147483647

 

enum sex:byte
{
    male=0;
    female=1;
}

enum sexy:int
{
   male=0;
   female=1;
}

sex A=sex.male;  //A在栈中占用一个字节的位置
sexy B=sexy.male //B在栈中占用四个字节的位置 因为int是4字节整数

二、枚举的继承关系:

是值类型,

继承至Enum,System.ValueType

三、Enum的一些类方法和实例方法。()

和其他类型之间的相互转化(Fruit)Enum.Parse(typeof(Fruit),”apple”);

获取所有的值Enum.GetValues(typeof(Fruit))

获取所有的成员的名称Enum.getNames(typeof(Fruit))

获取基础类型(默认为int):Enum.GetUnderlyingType(typeof(Fruit))

四、枚举的Description特性的使用:

作用:添加对应枚举的中文描述()

如何获取:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Linq;

using System.Reflection;

using System.Text;

using System.Threading.Tasks;

 

namespace ConsolePro

{

   

    [Description("水果")]

    public enum Fruit

    {

        [Description("苹果")]

        apple = 1,

        [Description("橘子")]

        orange = 2,

        [Description("香蕉")]

        banana = 3

    }

    class Program

    {

        static void Main(string[] args)

        {

 

            Console.WriteLine(Fruit.apple.GetDescription(false));

            Console.WriteLine(Fruit.apple.GetDescription(true));

            Console.Read();

        }

 

    }

    public static class Extension

    {

        public static string ToConcatString(this string[] strs, char concatChar)

        {

            StringBuilder build = new StringBuilder();

            foreach (var item in strs)

            {

                build.AppendFormat("{0}{1}", item, concatChar);

            }

            return build.ToString().TrimEnd(concatChar);

 

        }

        /// <summary>

        /// 获取枚举的Description特性的描述

        /// </summary>

        /// <param name="enumObj"></param>

        /// <param name="isEnumSelf">是否显示的enum类型上的Description还是当前枚举值的</param>

        /// <returns></returns>

        public static string GetDescription(this object enumObj, bool isEnumSelf)

        {

            string description = string.Empty;

            try

            {

                if (enumObj != null)

                {

                    Type enumType = enumObj.GetType();

                    DescriptionAttribute desAttr = null;

                    if (isEnumSelf)

                    {

                        desAttr = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute));

                    }

                    else

                    {

                        FieldInfo field = enumType.GetField(Enum.GetName(enumType, enumObj));

                        desAttr = (DescriptionAttribute)Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));

 

                    }

                    if (desAttr != null)

                    {

                        description = desAttr.Description;

                    }

                }

            }

            catch

            {

                return string.Empty;

            }

            return description;

        }

 

    }

}

 

 

https://www.cnblogs.com/xiaosongluffy/articles/3710333.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值