本文介绍了使用一个枚举在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?

推荐答案

如果您枚举项的值是contigious,数组方法非常有效。然而,在任何情况下,你可以使用词典< D​​ayOfTheWeek,串方式> (这是少高性能,顺便说一下)

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#中的数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:40