NameValueCollection class is in System.Collection.Specialized assembly.

Unlike with HashTable, NameValueCollection can have more than one values for one key.

So there is no error for code below:

NameValueCollection myCol = new NameValueCollection();

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

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

while using HashTable there will be an error.

Let us see some demo:

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

 public class ConsoleTest
 {

     public static void Main()
     {

         // Creates and initializes a new NameValueCollection.
         NameValueCollection myCol = new NameValueCollection();
         myCol.Add("red", "rojo");
         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]);
         Console.WriteLine("Key \"red\" has the value {0}.", myCol["red"]);
         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();

         // Searches for a key and deletes it.
         myCol.Remove("green");
         Console.WriteLine("The collection contains the following elements after removing \"green\":");
         PrintKeysAndValues(myCol);

         // Clears the entire collection.
         myCol.Clear();
         Console.WriteLine("The collection contains the following elements after it is cleared:");
         PrintKeysAndValues(myCol);

     }

     public static void PrintKeysAndValues(NameValueCollection myCol)
     {
         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();
     }

 }

 /*

 This code produces the following output.

 Displays the elements using the AllKeys property and the Item (indexer) property:
    KEY        VALUE
    red        rojo,rouge
    green      verde
    blue       azul

 Displays the elements using GetKey and Get:
    [INDEX] KEY        VALUE
    [0]     red        rojo,rouge
    [1]     green      verde
    [2]     blue       azul

 Index 1 contains the value verde.
 Key "red" has the value rojo,rouge.

 The string array contains:
    rojo,rouge
    verde
    azul

 The collection contains the following elements after removing "green":
    KEY        VALUE
    red        rojo,rouge
    blue       azul

 The collection contains the following elements after it is cleared:
    KEY        VALUE

 */

reference: https://msdn.microsoft.com/zh-cn/library/system.collections.specialized.namevaluecollection.aspx

.NET C#: NameValueCollection的更多相关文章

  1. NameValueCollection类集合

    1.NameValueCollection类集合是基于 NameObjectCollectionBase 类. 但与 NameObjectCollectionBase 不同,该类在一个键下存储多个字符 ...

  2. convert NameValueCollection/Dictionary<string, object> to JSON string

    public static class WebExtension { public static T Decode<T>(this RequestBase res) { Type type ...

  3. 以NameValueCollection 修改URL中的查询参数

    以NameValueCollection 修改URL中的查询参数 本文参考于:http://www.c-sharpcorner.com/Blogs/9421/add-remove-or-modify- ...

  4. NameValueCollection详解

    1.NameValueCollection类集合是基于 NameObjectCollectionBase 类. 但与 NameObjectCollectionBase 不同,该类在一个键下存储多个字符 ...

  5. NameValueCollection类

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

  6. (转)C# NameValueCollection集合

    1.NameValueCollection类集合是基于 NameObjectCollectionBase 类. 但与 NameObjectCollectionBase 不同,该类在一个键下存储多个字符 ...

  7. C#获取URL参数值(NameValueCollection)

    在写程序的时候,我们经常需要对页面进行传参数,比如page?id=1234,那么在page这个页面中就直接可以使用string id = Request.QueryString["id&qu ...

  8. C# NameValueCollection集合 .

    案例: NameValueCollection nameValueCollection = Request.Params;//获得连接地址中的所有参数 //获取各个参数,eg:            ...

  9. 字符串拼接 拆分 NameValueCollection qscoll = HttpUtility.ParseQueryString(result)

    string result = "sms&stat=100&message=发送成功"; string d = HttpUtility.ParseQueryStri ...

随机推荐

  1. Mysql 按行dump出数据

    mysqldump -u${user} -p${passwd} --skip-extended-insert --database ${dbname} --table ${tablename} > ...

  2. .SQL Server中 image类型数据的比较

    原文:.SQL Server中 image类型数据的比较 在SQL Server中如果你对text.ntext或者image数据类型的数据进行比较.将会提示:不能比较或排序 text.ntext 和 ...

  3. JS传中文到后台需要的处理

    前台JS使用encodeURI函数对中文进行编码. 后台Java使用URIDecoder.decode(str,UTF_8)函数对中文进行解码,之后获得中文原文.

  4. flex box 布局

    .box{ display:flex; } .box { display: inline-flex; } .box { display:-webkit-flex; display: flex; } f ...

  5. JavaScript:单选钮的事件处理

    单选按钮事件: 单选钮属于多选一的处理流程,但是单选钮由于也是HTML元素,所以对于JavaScript而言也表示对象. 注意:单选钮的特点是一定要需要名相同才可以实现,所以此处如果名字相同,那么就是 ...

  6. CentOS6.7搭建蜜罐dionaea

    yum -y install epel-release wget tar git autoconf* libtool-* mkdir /opt/dionaea 1.安装liblcfg软件.git cl ...

  7. ps、grep和kill联合使用杀掉进程(转)

    例如要杀掉hello这个进程,使用下面这个命令就能直接实现.   ps -ef |grep hello |awk '{print $2}'|xargs kill -9 这里是输出ps -ef |gre ...

  8. .net平台下深拷贝和浅拷贝

    在.net类库中,对象克隆广泛存在于各种类型的实现中,凡是实现了ICloneable接口的类型都具备克隆其对象实例的能力.所以本文讲述的深拷贝和浅拷贝也是在实现ICloneable接口的基础上进行的. ...

  9. window.open下载文件ie8请求的站点不可用的解决办法

    在业务里用到了PHP header导出doc文档,GET传值到页面,读出相应数据输出doc文件下载.用户提出需要批量,于是设计成js循环出对应数量的window.open(),向页面传入不同的值,批量 ...

  10. android点滴之ViewTreeObserver

    一类的基本概念 这是一个注册监听视图树的观察者(observer),在视图树种全局事件改变时得到通知.这个全局事件不仅还包括整个树的布局,从绘画过程开始,触摸模式的改变等.最常见的用途时通过监听获知什 ...