Using an enum as an array index in C#

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

    Compact form of enum used as index and assigning whatever type to a Dictionary and strongly typed. In this case float values are returned but values could be complex Class instances having properties and methods and more:

    enum opacityLevel { Min, Default, Max }
    private static readonly Dictionary _oLevels = new Dictionary
    {
        { opacityLevel.Max, 40.0 },
        { opacityLevel.Default, 50.0 },
        { opacityLevel.Min, 100.0 }
    };
    
    //Access float value like this
    var x = _oLevels[opacitylevel.Default];
    

提交回复
热议问题