.NET C#: NameValueCollection
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的更多相关文章
- NameValueCollection类集合
1.NameValueCollection类集合是基于 NameObjectCollectionBase 类. 但与 NameObjectCollectionBase 不同,该类在一个键下存储多个字符 ...
- convert NameValueCollection/Dictionary<string, object> to JSON string
public static class WebExtension { public static T Decode<T>(this RequestBase res) { Type type ...
- 以NameValueCollection 修改URL中的查询参数
以NameValueCollection 修改URL中的查询参数 本文参考于:http://www.c-sharpcorner.com/Blogs/9421/add-remove-or-modify- ...
- NameValueCollection详解
1.NameValueCollection类集合是基于 NameObjectCollectionBase 类. 但与 NameObjectCollectionBase 不同,该类在一个键下存储多个字符 ...
- NameValueCollection类
最近在研究HttpRequest类,发现里面的很多属性都返回一个NameValueCollection对象,今天再来了解一下这个神秘的对象. 随便写了个例子,发现跟HashTable类似.但是这个东西 ...
- (转)C# NameValueCollection集合
1.NameValueCollection类集合是基于 NameObjectCollectionBase 类. 但与 NameObjectCollectionBase 不同,该类在一个键下存储多个字符 ...
- C#获取URL参数值(NameValueCollection)
在写程序的时候,我们经常需要对页面进行传参数,比如page?id=1234,那么在page这个页面中就直接可以使用string id = Request.QueryString["id&qu ...
- C# NameValueCollection集合 .
案例: NameValueCollection nameValueCollection = Request.Params;//获得连接地址中的所有参数 //获取各个参数,eg: ...
- 字符串拼接 拆分 NameValueCollection qscoll = HttpUtility.ParseQueryString(result)
string result = "sms&stat=100&message=发送成功"; string d = HttpUtility.ParseQueryStri ...
随机推荐
- 【转】JavaScript中的this关键字使用的四种调用模式
http://blog.csdn.net/itpinpai/article/details/51004266 this关键字本意:这个.这里的意思.在JavaScript中是指每一个方法或函数都会有一 ...
- day11
JSP入门 1 JSP概述 1.1 什么是JSP JSP(Java Server Pages)是JavaWeb服务器端的动态资源.它与html页面的作用是相同的,显示数据和获取数据. 1.2 ...
- Qt调用Server SQL中的存储过程
Server SQL中的存储过程如下: CREATE procedure PINSERTPC @pcnum int, @pcname varchar(50), @pctype int, @ipaddr ...
- 50道JavaScript经典题和解法(JS新手进...持续更新...)
最近在学习<数据结构与算法JavaScript描述>这本书,对JavaScript的特性和数据结构都有了进一步的了解和体会. 学习之余,也进行了相应的练习,题目难度不大,但是对所学知识的巩 ...
- Four Operations---hdu5938(暴力)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5938 题意:给一个不超过20位并且大于2位的数字字符串(只包含1-9)然后找到适当的位置依次放入&qu ...
- Echart - 地图散点图(服务网点图)的实现
Echart是百度开发的一个javascript图表库,可以流程运行于pc和移动端,底层依赖轻量级的 Canvas 类库 ZRender. ECharts 提供了常规的折线图,柱状图,散点图,饼图,K ...
- JS之tagNaem和nodeName
nodeName是节点的属性,tagName是元素的属性.元素是节点的子集.不是任何节点都有tagName的,比如文本节点,仅有nodeName属性. 这个和css中的倾斜和斜体的关系是一样的.不是所 ...
- javac 错误: 编码GBK的不可映射字符
在java代码中有中文注释,使用javac编译时,出现编码报错. 错误: 编码GBK的不可映射字符 问题原因: 在编译的时候,如果我们没有用-encoding参数指定我们的JAVA源程序的编码格式,则 ...
- iOS 时间戳
时间戳用过一回,总结一下: 1.在Terminal下获得时间戳: date +%s 原来要在服务器端挂文档,方便开发商下载,用时间戳作为文件名称来区分. 2.在程序下获得时间戳: NSDate * n ...
- JS-007-富文本域操作
在日常 web 编写过程中,富文本域几乎成为了一个网站不可页面元素,同时,其也有着各种各样的实现方式,网络上也存在着各种各样的集成插件可供引用.此文以 js 获取.修改 163 邮箱写邮件时的邮件内容 ...