class与Enum中的ToString()的处理

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    public class OrderState
    {
        private OrderState() { }  
        public static OrderState Confirm  = new OrderState();
        public static OrderState New = new OrderState();
        public override string ToString()
        {
            if (this.Equals(OrderState.Confirm))
            {
                return "确定";
            }
            else if (this.Equals(OrderState.New))
            {
                return "新订单";
            }
            return null;
            //return base.ToString();
        }
        public static OrderState ToEnum(string value)
        {
            if (value == "确定")
            {
                return OrderState.Confirm;
            }
            else if (value == "新订单")
            {
                return OrderState.New;
            }
            return OrderState.New;
            //return null;
        }
    }
    public class Test
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(OrderState.Confirm);
            Console.WriteLine(OrderState.ToEnum("确定"));
            Console.WriteLine(OrderStateE.Confirm);
            Console.WriteLine(EnumHelper.ToString(OrderStateE.Confirm));
            Console.WriteLine(EnumHelper.ToEnumOrderState("确定"));
        }
    }
    public enum OrderStateE : byte
    {
        Confirm = 0,
        New = 1
    }
    public class EnumHelper
    {
        private static readonly string[] OrderStateArr ={ "确定", "新订单" };
        public static string ToString(OrderStateE orderState)
        {
            return OrderStateArr[(int)orderState];
        }
        public static OrderStateE ToEnumOrderState(string value)
        {
            for (int i = 0; i < OrderStateArr.Length; i++)
            {
                if (value == OrderStateArr[i])
                {
                    return (OrderStateE)i;
                }
            }
            return OrderStateE.Confirm;
        }
    }

    public class OrderStateConst
    {
        //没有类型安全,没有范围,但设计最简单
        public const string Confirm = "确定";
        public const string New = "新订单";       
    }
}

 

 

 

========================

枚举是值类型 和类不一样,所以在效率上在些差异

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值