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. IoC容器装配Bean(xml配置方式)(Bean的生命周期)

    1.Spring管理Bean,实例化Bean对象 三种方式 第一种:使用类构造器实例化(默认无参数) package cn.itcast.spring.initbean; /** * 使用构造方法 实 ...

  2. Python中dict的特点、更新dict、遍历dict

    dict的第一个特点是查找速度快,无论dict有10个元素还是10万个元素,查找速度都一样.而list的查找速度随着元素增加而逐渐下降. 不过dict的查找速度快不是没有代价的,dict的缺点是占用内 ...

  3. JVM GC (一)

    1. 先来说GC工作在哪块区域呢? 程序计数器,虚拟机栈(也就平时所说的栈), 本地方法栈这三区域随着线程而生,随着线程而灭,出栈入栈的操作,在栈中分配配的多少内存都具 有确定性,在这几个区域就不用考 ...

  4. php--tp中页面之间的跳转

  5. IdTcpClient简单示例

    procedure TForm1.btnHttpGetClick(Sender: TObject); begin idtcpclnt1.Host := '192.168.10.88'; idtcpcl ...

  6. android通过pc脚本执行sqlite3脚本

    最近在调研市面上的一些android db框架,需要经常重复的输入一堆比如 adb shell cd /data/data/com.example.testandroiddb/databases sq ...

  7. mysql存储过程之游标遍历数据表

    原文:mysql存储过程之游标遍历数据表 今天写一个mysql存储过程,根据自己的需求要遍历一个数据表,因为对存储过程用的不多,语法不甚熟悉,加之存储过程没有调试环境,花了不少时间才慢慢弄好,故留个痕 ...

  8. Magento: How to reset admin pssword

    Magento: How to reset admin pssword If you forget your admin password for Magento and you can’t reme ...

  9. 【C++】虚函数

    I 动态绑定.多态.虚函数.对象的静态类型与动态类型 1.基类中有两种函数: 派生类直接继承不做改变 派生类重新定义成适合自身的版本覆盖掉基类的函数 对于第一种就是普通的基类成员函数,第二种通常通过将 ...

  10. ie8兼容border-radius方法

    <!doctype html><html> <head>        <meta charset="utf-8" />    &l ...