how can i use switch statement on type-safe enum pattern

后端 未结 3 1174
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 09:51

I found a goodlooking example about implementation enums in a different way. That is called type-safe enum pattern i think. I started using it but i realized th

3条回答
  •  日久生厌
    2021-02-05 10:14

    You can try something like this:

    class Program
    {
        static void Main(string[] args)
        {
            Gender gender = Gender.Unknown;
    
            switch (gender)
            {
                case Gender.Enum.Male:
                    break;
                case Gender.Enum.Female:
                    break;
                case Gender.Enum.Unknown:
                    break;
            }
        }
    }
    
    public class Gender : NameValue
    {
        private Gender(int value, string name)
            : base(value, name)
        {
        }
    
        public static readonly Gender Unknown = new Gender(Enum.Unknown, "Unknown");
        public static readonly Gender Male = new Gender(Enum.Male, "Male");
        public static readonly Gender Female = new Gender(Enum.Female, "Female");
        public class Enum
        {
            public const int Unknown = -1;
            public const int Male = 1;
            public const int Female = 2;
        }
    
    }
    
    public abstract class NameValue
    {
        private readonly int _value;
        private readonly string _name;
    
        protected NameValue(int value, string name)
        {
            _value = value;
            _name = name;
        }
    
        public int Value
        {
            get { return _value; }
        }
    
        public string Name
        {
            get { return _name; }
        }
    
        public override string ToString()
        {
            return Name;
        }
        public override int GetHashCode()
        {
            return Value.GetHashCode();
        }
    
        public override bool Equals(object obj)
        {
            NameValue other = obj as NameValue;
            if (ReferenceEquals(other, null)) return false;
            return this.Value == other.Value;
        }
    
        public static implicit operator int(NameValue nameValue)
        {
            return nameValue.Value;
        }
    }
    

提交回复
热议问题