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 = "新订单";
}
}
========================
枚举是值类型 和类不一样,所以在效率上在些差异