Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c#中创建属性类?_C#_Dictionary_Hashtable - Fatal编程技术网

如何在c#中创建属性类?

如何在c#中创建属性类?,c#,dictionary,hashtable,C#,Dictionary,Hashtable,我想创建一个具有两个属性的类,例如key和value 我想要一种方法,根据键给我一个值 那么代码是什么?我知道Hashtable,但是如何在C中实现它呢?我可以用一个字符串作为键吗?看看字典类:使用字典字典可以做你想做的一切。下面是实现这一点的最佳方法(我使用Int32作为存储类型的示例): 还有几个实现。有一个是为集合操作而设计的,它是一个易于序列化的哈希表。。。。还有System.Collections.Specialized.NameValueCollection,它大致相当于字典,但允许

我想创建一个具有两个属性的类,例如
key
value

我想要一种方法,根据
给我一个


那么代码是什么?我知道Hashtable,但是如何在C中实现它呢?我可以用一个
字符串作为键吗?

看看
字典
类:

使用
字典
字典
可以做你想做的一切。

下面是实现这一点的最佳方法(我使用Int32作为存储类型的示例):


还有几个实现。有一个是为集合操作而设计的,它是一个易于序列化的哈希表。

。。。还有
System.Collections.Specialized.NameValueCollection
,它大致相当于
字典
,但允许在同一键值下存储多个字符串值。引用MSDN文档:

此类可用于标题, 查询字符串和表单数据

Dictionary<String,Int32> dictionary = new Dictionary<String,Int32>
{
    // this just loads up the list with
    // some dummy data - notice that the
    // key is a string and the value is an int
    { "one", 1 },
    { "two", 2 },
    { "three", 3 },
};
dictionary["one"]; // returns 1
dictionary["two"]; // returns 2