您的位置:首页 > 产品设计 > UI/UE

NameValueCollection详解

2013-10-03 19:49 344 查看

1.NameValueCollection类集合是基于 NameObjectCollectionBase 类。

但与 NameObjectCollectionBase 不同,该类在一个键下存储多个字符串值(就是键相同,值就连接起来如下例子)。该类可用于标头、查询字符串和窗体数据。
每个元素都是一个键/值对。NameValueCollection 的容量是 NameValueCollection 可以保存的元素数。
NameValueCollection 的默认初始容量为零。随着向 NameValueCollection 中添加元素,容量通过重新分配按需自动增加。
如下例子:

NameValueCollection myCol = new NameValueCollection();

myCol.Add("red", "rojo");//如果键值red相同结果合并 rojo,rouge

myCol.Add("green", "verde");

myCol.Add("blue", "azul");

myCol.Add("red", "rouge");

2.NameValueCollection与Hashtable的区别

a.引用区别

hashtable:using System.Collections;

NameValueCollection:using System.Collections.Specialized;

b.键是否重复

NameValueCollection:允许重复.

HashTable是键-值集合,但键不能出现重复.

Hashtable ht = new Hashtable();

ht.Add("key","value");

ht.Add("key", "value1"); //出错

ht["key"] = "value1"; //正确

NameValueCollection遍历

与Hashtable相似:

NameValueCollection myCol = new NameValueCollection();

myCol.Add("red", "rojo");//如果键值red相同结果合并 rojo,rouge

myCol.Add("green", "verde");

myCol.Add("blue", "azul");

myCol["red"] = "dd";

foreach (string key in myCol.Keys)

{

Console.WriteLine("{0}:{1}", key, myCol[key]);

}

Console.ReadLine();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: