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. php数据库两个关联大表的大数组分页处理,防止内存溢出

    $ret = self::$db->select($tables, $fields, $where, $bind); if (!empty($ret)) { $retIds = array(); ...

  2. SQL2005的cte递归查询子树

    ;with cteas(select id,caption,parentid,1 Gen from skywfflow where parentid =0UNION ALL select a.id,a ...

  3. 使用Reveal

    添加Reveal.framework,设置Other link flags 添加Debug为 -ObjC,添加 libz 库 这里介绍 Reveal UI 分析工具的简单使用,至于使用他分析手机 Ap ...

  4. struts json登录

    1.struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts P ...

  5. zepto源码--filtered, contains,funcArg,setAttribute,className,deserializeVale--学习笔记

    几个方法 1.filtered 目标是对节点按照一定的选择器进行过滤. 如果传入了过滤选择器,则在nodes节点下,选择符合选择器的节点: 如果没有传入选择器,则返回节点本身,转化为zepto节点. ...

  6. 【C++】error C4146: 一元负运算符应用于无符号类型,结果仍为无符号类型

    刷leetcode 263.uglynumber时,代码如下: class Solution { public: bool isUgly(int num) { int temp = num; ) re ...

  7. Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树

    A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...

  8. Hashtable,HashMap,Dictionary的区别

    Hashtable和HashMap的区别:1.Hashtable是基于Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现,c#中无HashMap2.Hashtable ...

  9. 批量处理_cmd_matlab

    cd \ cd D:\Projects_Face_Detection\Datasets\afw d: dir /b/s/p/w *jpg > Path_Images.txt 1.ground_t ...

  10. vsUnit单元测试

    在自定义的方法名上[右键]然后选择[创建单元测试],之后在项目中就添加了一个单元测试的项目,找到对应的单元测试的方法[TestMethod()]特性修饰,将单元测试的方法中最后一句:Assert.In ...