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 sql技巧篇

    1.left join 需要注意的事项 以左表为基准,匹配右表,如果右表匹配了两条,那么,就生成两条记录,而这两条记录的坐表信息都是一样的. 之前误以为,右表不会影响记录的条数.select 部分,不 ...

  2. SQLite(快速上手版)笔记

    原文 1. SQL语法关键字 关键字 描述 Create Table 创建数据表 Alter Table 修改数据表 Drop Table 删除数据表 Create Index 创建索引 Drop I ...

  3. jboss4.2.3建立oracle JMS应用

    一.基本配置 1 增加oracle驱动文件,ojdbc6.jar,不能使用小于该版本的jdbc驱动,jboss-4.2.3.GA\server\default\lib 2 增加retrotransla ...

  4. bootstrap响应式实用工具

  5. C# 字符串的截取和替换

    1.取字符串的前n个字符 (1)string str1=str.Substring(0,n); (2)string str1=str.Remove(i,str.Length-n); 2.去掉字符串的前 ...

  6. H264(NAL简介与I帧判断)

    1.NAL全称Network Abstract Layer, 即网络抽象层.         在H.264/AVC视频编码标准中,整个系统框架被分为了两个层面:视频编码层面(VCL)和网络抽象层面(N ...

  7. ArcGIS API for Silverlight代码中使用Template模板

    原文:ArcGIS API for Silverlight代码中使用Template模板 在项目开发中,会遇到点选中聚焦闪烁效果,但是因为在使用Symbol的时候,会设置一定的OffSetX和OffS ...

  8. MVC中Linq to sql创建数据模型

    1.创建新的 SQL Server 数据库 点击”视图“-->“服务器资源管理器” ,打开 “服务器资源管理器” 窗口,如下图: 右键“数据连接”,选择“创建新的SQL Server 数据库”, ...

  9. python chr() unichr() ord()

    了解一下python chr(),unichr(),ord()函数的用法. 参考链接: http://crazier9527.iteye.com/blog/411001 chr() 输入参数(取值范围 ...

  10. Java Difference between Private and Protected

    Private means this could only be seen within this class. Protected means "package private" ...