Using an enum as an array index in C#

前端 未结 10 793
死守一世寂寞
死守一世寂寞 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:40

    Here you go:

    string[] message_array = Enum.GetNames(typeof(DaysOfTheWeek));
    

    If you really need the length, then just take the .Length on the result :) You can get values with:

    string[] message_array = Enum.GetValues(typeof(DaysOfTheWeek));
    

提交回复
热议问题