I would like to store a enum value and a number in a collection or array, with the ability to update a specific value. Any recommendations on best method, taken into account performance impact?
我想在集合或数组中存储枚举值和数字,并能够更新特定值。关于最佳方法的任何建议,考虑到性能影响?
E.g.
enum Colour {Red, Yellow, Green};
Would like to store (and update):
想存储(和更新):
Red: 230
Yellow: 500
Green: 50
2 个解决方案
#1
1
You could use a Dictionary<Colour, int>
:
你可以使用Dictionary
Dictionary<Colour, int> dict = new Dictionary<Colour, int>();
dict[Colour.Red] = 230;
dict[Colour.Yellow] = 500;
dict[Colour.Green] = 50;
Console.WriteLine(dict[Colour.Red]) //outputs 230
#2
1
The best solution is to use a dictionary:
最好的解决方案是使用字典:
var dictionary = new Dictionary<Colour, int>()
dictionary[Colour.Red] = 230;
dictionary[Colour.Yellow] = 500;
dictionary[Colour.Green] = 50;
Doing this, every colour exists at maximum once in the collection. If you're using a Collection
or an Array
, you'll be able to have multiple times the same colour in the collection.
这样做,每个颜色在集合中最多存在一次。如果您使用的是Collection或Array,则可以在集合中多次使用相同的颜色。
#1
1
You could use a Dictionary<Colour, int>
:
你可以使用Dictionary
Dictionary<Colour, int> dict = new Dictionary<Colour, int>();
dict[Colour.Red] = 230;
dict[Colour.Yellow] = 500;
dict[Colour.Green] = 50;
Console.WriteLine(dict[Colour.Red]) //outputs 230
#2
1
The best solution is to use a dictionary:
最好的解决方案是使用字典:
var dictionary = new Dictionary<Colour, int>()
dictionary[Colour.Red] = 230;
dictionary[Colour.Yellow] = 500;
dictionary[Colour.Green] = 50;
Doing this, every colour exists at maximum once in the collection. If you're using a Collection
or an Array
, you'll be able to have multiple times the same colour in the collection.
这样做,每个颜色在集合中最多存在一次。如果您使用的是Collection或Array,则可以在集合中多次使用相同的颜色。