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. RelativeLayout不能调用measure去直接测量子元素

    RelativeLayout在实现onMeasure方法时并没有像LinearLayout一样去重写如下代码: @Override protected void onMeasure(int width ...

  2. Java 进程(转)

    转自http://jiangshuiy.iteye.com/blog/1674235 PS:今天做android助手项目的时候,发现adb push命令执行会卡死,最后发现不能用waitfor阻塞等待 ...

  3. 判断cdn上的图片可以正常访问到

    昨天晚上cdn宕机1小时,要对上传的资检查,写了个简单的小脚本来实现上传过的资源都是正常的(其实非必须),就是练手防止生疏. arr.each do |a | res = Net::HTTP.get_ ...

  4. 不允许修改SQLserver2008r2表中字段的属性问题

    SQLserver2008r2修改表中字段的属性时弹出 点击工具->选项,取消阻止保存要求重新创建表的更改

  5. emmc boot1 boot2 partition

    使用mfg tool烧写android5.1的镜像之后,再使用旧版的mfg tool烧写linux或者android镜像,都不能正常启动,而且运行的uboot还是android5.1版本的uboot. ...

  6. web系统权限设计

    应该有七张表 1.appSystem 表: 主要在多系统中的 统一权限管理:主要就是系统的URL USE [Star_Permission] GO /****** 对象: Table [dbo].[A ...

  7. 移动端a链接点击时取出背景色及边框

    a{blr:expression(this.onFocus=this.blur())} :focus{outline:0;} /*去掉a标签的虚线框,避免出现奇怪的选中区域*/*{-webkit-ta ...

  8. MySQL: 详细的sql语句

    1添 1.1[插入单行] insert [into] <表名> (列名) values (列值)例:insert into Strdents (姓名,性别,出生日期) values ('开 ...

  9. 使用openvswitch实现跨主机docker容器互联

    安装openvswitch的步骤请参考上一篇文章:http://www.cnblogs.com/xkops/p/5568167.html 环境:192.168.3.201 node1192.168.3 ...

  10. jquery实现"跳到底部","回到顶部"效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...