Using an enum as an array index in C#

前端 未结 10 972
死守一世寂寞
死守一世寂寞 2020-12-28 14:17

I want to do the same as in this question, that is:

enum DaysOfTheWeek {Sunday=0, Monday, Tuesday...};
string[] message_array = new string[number_of_items_at         


        
10条回答
  •  孤独总比滥情好
    2020-12-28 14:27

    You could make a class or struct that could do the work for you


    public class Caster
    {
        public enum DayOfWeek
        {
            Sunday = 0,
            Monday,
            Tuesday,
            Wednesday,
            Thursday,
            Friday,
            Saturday
        }
    
        public Caster() {}
        public Caster(string[] data) { this.Data = data; }
    
        public string this[DayOfWeek dow]{
            get { return this.Data[(int)dow]; }
        }
    
        public string[] Data { get; set; }
    
    
        public static implicit operator string[](Caster caster) { return caster.Data; }
        public static implicit operator Caster(string[] data) { return new Caster(data); }
    
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Caster message_array = new string[7];
            Console.Write(message_array[Caster.DayOfWeek.Sunday]);
        }
    }
    

    EDIT

    For lack of a better place to put this, I am posting a generic version of the Caster class below. Unfortunately, it relies on runtime checks to enforce TKey as an enum.

    public enum DayOfWeek
    {
        Weekend,
        Sunday = 0,
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday
    }
    
    public class TypeNotSupportedException : ApplicationException
    {
        public TypeNotSupportedException(Type type)
            : base(string.Format("The type \"{0}\" is not supported in this context.", type.Name))
        {
        }
    }
    
    public class CannotBeIndexerException : ApplicationException
    {
        public CannotBeIndexerException(Type enumUnderlyingType, Type indexerType)
            : base(
                string.Format("The base type of the enum (\"{0}\") cannot be safely cast to \"{1}\".",
                              enumUnderlyingType.Name, indexerType)
                )
        {
        }
    }
    
    public class Caster
    {
        private readonly Type baseEnumType;
    
        public Caster()
        {
            baseEnumType = typeof(TKey);
            if (!baseEnumType.IsEnum)
                throw new TypeNotSupportedException(baseEnumType);
        }
    
        public Caster(TValue[] data)
            : this()
        {
            Data = data;
        }
    
        public TValue this[TKey key]
        {
            get
            {
                var enumUnderlyingType = Enum.GetUnderlyingType(baseEnumType);
                var intType = typeof(int);
                if (!enumUnderlyingType.IsAssignableFrom(intType))
                    throw new CannotBeIndexerException(enumUnderlyingType, intType);
                var index = (int) Enum.Parse(baseEnumType, key.ToString());
                return Data[index];
            }
        }
    
        public TValue[] Data { get; set; }
    
    
        public static implicit operator TValue[](Caster caster)
        {
            return caster.Data;
        }
    
        public static implicit operator Caster(TValue[] data)
        {
            return new Caster(data);
        }
    }
    
    // declaring and using it.
    Caster messageArray =
        new[]
            {
                "Sunday",
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday"
            };
    Console.WriteLine(messageArray[DayOfWeek.Sunday]);
    Console.WriteLine(messageArray[DayOfWeek.Monday]);
    Console.WriteLine(messageArray[DayOfWeek.Tuesday]);
    Console.WriteLine(messageArray[DayOfWeek.Wednesday]);
    Console.WriteLine(messageArray[DayOfWeek.Thursday]);
    Console.WriteLine(messageArray[DayOfWeek.Friday]);
    Console.WriteLine(messageArray[DayOfWeek.Saturday]);
    

提交回复
热议问题