简体   繁体   English

从表添加和检索键值对

[英]Add and retrieve Key Value pair from table

I have a (read only) table containing some config info, for example: 我有一个(只读)表,其中包含一些配置信息,例如:

Red  -  1,
Green - 2,
Orange - 3.

What is the easiest way to load this into some collection, so that I can write, for example: 将其加载到某个集合中的最简单方法是什么,以便我可以编写例如:

byte x = MyColl.Red;

One option would be to use an enum 一种选择是使用枚举

The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list. enum关键字用于声明枚举,这是一种独特的类型,由一组称为枚举器列表的命名常量组成。

The problem with this is that it needs to be hard coded at compile time, so reading it from a table will not make sense. 问题在于它需要在编译时进行硬编码,因此从表中读取它是没有意义的。

The other option would be tio use Dictionary Class 另一个选择是tio use Dictionary Class

Represents a collection of keys and values. 表示键和值的集合。

where you could look at 你可以看的地方

Dictionary<string, byte>

So as to use it such that 以便使用它

byte x = MyCollDictionary["Red"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM