.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 ...
随机推荐
- 获取真实ip的报告
今天登录九秒社团 http://www.9miao.com/的时候忘记了用户名和密码,尝试了5次都没登录成功,网站弹出提示15分钟后才能再次登录.我纳闷它是怎么判断用户的登录次数,这时候用户还没有登录 ...
- BlueDroid介绍
目录 1. 基本结构 2. 代码区 自从Android 4.2开始,Android开始使用自己的蓝牙协议栈BlueDroid,而不是bluez BlueDroid可分为两层: - BTE: Bluet ...
- MySQL- 锁(1)
锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是所有数 ...
- 理解tornado
计算密集型:多进程 IO密集型:多线程 能产生IO阻塞的情况很多,比如网络通讯.磁盘读写.当发生阻塞时,CPU是闲置的,此时如果就一个线程就没法处理其他事情了. 所以对于含有IO阻塞的环境,多线程 ...
- JNI字段描述符-Java Native Interface Field Descriptors
一.JNI字段描述符 "[I" --- int[] "[[[D" --- double[][][] 如果以一个L开头的描述符,就是类描述符,它后紧跟着类的字符 ...
- 超级管理员登录后如果连续XX分钟没有操作再次操作需要重新登录
首先在设置session页面时 session_start(); session("name",$adminname); //加入session时间 time() session( ...
- 闭包 Clousure
闭包 Clousure 参考:http://caibaojian.com/javascript-closures.html?fid=776%230-tsina-1-25974-397232819ff9 ...
- NSURLConnection同步与异步请求 问题
NSURLConnection目前有两个异步请求方法,异步请求中其中一个是代理.一个同步方法.有前辈已经详细介绍,见:http://blog.csdn.net/xyz_lmn/article/deta ...
- 实验 snort安装配置与规则编写
1 实验目的 在linux或windows任意一个平台下完成snort的安装,使snort工作在NIDS模式下,并编写符合相关情景要求的snort规则. 2 实验环境 物理机:windows 8.1 ...
- SqlServer2008R2安装步骤
参考http://jimshu.blog.51cto.com/3171847/585023/