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"; //正确

3.初始化NameValueCollection
初始化NameValueCollection需引用using System.Collections.Specialized;
完整例子源码:

using System; using System.Collections;
using System.Collections.Specialized;

namespace SamplesNameValueCollection
{
class Program
{

public static void Main()
{
//初始化NameValueCollection需引用using System.Collections.Specialized;
NameValueCollection myCol = new NameValueCollection();
myCol.Add("red", "rojo");//如果键值red相同结果合并 rojo,rouge
myCol.Add("green", "verde");
myCol.Add("blue", "azul");
myCol.Add("red", "rouge");

// Displays the values in the NameValueCollection in two different ways.
//显示键,值
Console.WriteLine("Displays the elements using the AllKeys property and the Item (indexer) property:");
PrintKeysAndValues(myCol);
Console.WriteLine("Displays the elements using GetKey and Get:");
PrintKeysAndValues2(myCol);

// Gets a value either by index or by key.
//按索引或值获取
Console.WriteLine("Index 1 contains the value {0}.", myCol[1]);//索引1的值
Console.WriteLine("Key \"red\" has the value {0}.", myCol["red"]);//键为red的对应值rouge
Console.WriteLine();

// Copies the values to a string array and displays the string array.
String[] myStrArr = new String[myCol.Count];
myCol.CopyTo(myStrArr, 0);
Console.WriteLine("The string array contains:");
foreach (String s in myStrArr)
Console.WriteLine(" {0}", s);
Console.WriteLine();

//查找green键值然后删除
myCol.Remove("green");
Console.WriteLine("The collection contains the following elements after removing \"green\":");
PrintKeysAndValues(myCol);

//清空集合
myCol.Clear();
Console.WriteLine("The collection contains the following elements after it is cleared:");
PrintKeysAndValues(myCol);

}
//显示键,值
public static void PrintKeysAndValues(NameValueCollection myCol)
{
IEnumerator myEnumerator = myCol.GetEnumerator();
Console.WriteLine(" KEY VALUE");
foreach (String s in myCol.AllKeys)
Console.WriteLine(" {0,-10} {1}", s, myCol[s]);
Console.WriteLine();
}
//显示索引, 键,值
public static void PrintKeysAndValues2(NameValueCollection myCol)
{
Console.WriteLine(" [INDEX] KEY VALUE");
for (int i = 0; i < myCol.Count; i++)
Console.WriteLine(" [{0}] {1,-10} {2}", i, myCol.GetKey(i), myCol.Get(i));
Console.WriteLine();
}
}

}
4.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();

NameValueCollection类集合的更多相关文章

  1. NameValueCollection类读取配置信息

    C#中的NameValueCollection类读取配置信息,大家可以参考下.   我首先介绍配置文件中的写法: 1.在VS2015中的工程下建立一个控制台应用程序,其config文件默认名称为App ...

  2. 第八节:详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架

    前言 大家好,给大家带来详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架的概述,希望你们喜欢 JAVA 异常 try...catch...finally结构的使用方法 class Tes ...

  3. 多个module实体类集合打一个jar包并上传至远程库

    本章内容主要分享多个module中的实体类集合生成到一个jar包中,并且发布到远程库:这里采用maven-assembly-plugin插件的功能来操作打包,内容不长却贴近实战切值得拥有,主要节点内容 ...

  4. Java开发工具类集合

    Java开发工具类集合 01.MD5加密工具类 import java.security.MessageDigest; import java.security.NoSuchAlgorithmExce ...

  5. NameValueCollection类

    最近在研究HttpRequest类,发现里面的很多属性都返回一个NameValueCollection对象,今天再来了解一下这个神秘的对象. 随便写了个例子,发现跟HashTable类似.但是这个东西 ...

  6. MVC控制器传递多个实体类集合到视图的方案总结

    MVC控制器向视图传递数据包含多个实体类的解决方案有很多,这里主要针对视图模型.动态模型以及Tuple三种方法进行一些总结与记录. 基础集合类:TableA namespace ViewModelSt ...

  7. [转]Java常用工具类集合

    转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...

  8. 创建泛类集合List以及数组转集合,集合转数组的应用

    List<int> list = new List<int>(); list.Add(); list.Add(); list.Add(); list.AddRange(, , ...

  9. 用list<类>集合接收一个网址返回的一个类的集合的XML

    JavaScriptSerializer serializer = new JavaScriptSerializer(); string json = Share.Helper.HttpRequest ...

随机推荐

  1. Win7 32bit + Matlab2013b +Visual Studio 2010联合编程配置

    要建立独立运行的C应用程序,系统中需要安装Matlab.Matlab编译器.C/C++编译器以及Matlab C/C++数学库函数和图形库函数. Matlab编译器使用mbuild命令可以直接将C/C ...

  2. 使用Eclipse进行远程调试【转】

    今天决定做件有意义的事,写篇图文并茂的blog,为什么要图文并茂?因为很多事可能用语言也说不明白,从以前我发表的一篇文章可以看得出来,http://blog.csdn.net/sunyujia/arc ...

  3. sudo 使用不了, the permissions on the /etc/sudoers file are changed to something other than 0440

    sudo 使用不了,报错: the permissions on the /etc/sudoers file are changed to something other than 0440 how ...

  4. Selenium WebDriver屏幕截图(C#版)

    Selenium WebDriver屏幕截图(C#版)http://www.automationqa.com/forum.php?mod=viewthread&tid=3595&fro ...

  5. Nginx模块开发时unknown directive "echo"的处理

    实际上,Nginx并没有echo这个指令,所以你贸然使用时,自然会提示说无法识别的指令,处理方法有两个: 方法一是: 从下面连接下载echo-nginx-module模块并安装: https://gi ...

  6. POJ 1816 Wild Words

    Wild Words Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4412   Accepted: 1149 Descri ...

  7. Razor 在WebApp 框架的运用

    前面有两章介绍了WebApp框架<WebApp MVC,“不一样”的轻量级互联网应用程序开发框架>和<WebApp MVC 框架的开发细节归纳>,其中视图引擎是用的Nveloc ...

  8. Reveal查看任意app的高级技巧(转)

    原文:http://zhuanlan.zhihu.com/iOSRe/19646016 Reveal查看任意app的高级技巧 hangcom · 12 小时前 Reveal是一个很强大的UI分析工具, ...

  9. dSYM atos crash log 定位到代码行的方法(转)

    做iOS开发的时候,常常会遇到crash,需要分析call stack的时候.有时候App在别人的设备崩溃,把crash report在自己的机器上打开,Xcode没有自动的进行符号化.这时候就需要自 ...

  10. undefined function mysql_connect()解决方法

    在配置apache+php+mysql后,打开一个php网页文件正常,但是php网页中连接数据库时,出现以下提示: Fatal error: Call to undefined function my ...