在C#中使用枚举作为数组索引 [英] Using an enum as an array index in C#
本文介绍了在C#中使用枚举作为数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要执行与此问题相同的操作,那就是:
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_enum];
...
Console.Write(custom_array[(int)DaysOfTheWeek.Sunday]);
但是,我宁愿有一些内在的东西,而不是写这个错误的代码。在C#中有一个内置的模块,只是这样吗?
however, I would rather have something integral to so, rather than write this error prone code. Is there a built in module in C# that does just this?
推荐答案
如果枚举项的值是连续的,方法工作得很好。但是,无论如何,您可以使用字典< DayOfTheWeek,字符串>
(其性能较差)。
If the values of your enum items are contigious, the array method works pretty well. However, in any case, you could use Dictionary<DayOfTheWeek, string>
(which is less performant, by the way).
这篇关于在C#中使用枚举作为数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文