C# NameValueCollection
一个简单的例子
NameValueCollection markStatus = new NameValueCollection();
string[] values = null; markStatus.Add("Very High", "80");
markStatus.Add("High", "60");
markStatus.Add("medium", "50");
markStatus.Add("Pass", "40"); foreach (string key in markStatus.Keys)
{
values = markStatus.GetValues(key);
foreach (string value in values)
{
MessageBox.Show (key + " - " + value);
}
}
微软网站上的例子
using System;
using System.Collections;
using System.Collections.Specialized;
public class SamplesNameValueCollection { 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
*/
C# NameValueCollection的更多相关文章
- NameValueCollection类集合
1.NameValueCollection类集合是基于 NameObjectCollectionBase 类. 但与 NameObjectCollectionBase 不同,该类在一个键下存储多个字符 ...
- .NET C#: NameValueCollection
NameValueCollection class is in System.Collection.Specialized assembly. Unlike with HashTable, NameV ...
- 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 ...
随机推荐
- Objective-C:除数为0的情况下异常的处理(检测、抛出、捕捉、处理)
// DivTest.h // 异常的处理 // // Created by ma c on 15/8/11. // Copyright (c) 2015年. All rights reserved. ...
- 算法导论第九章 第K顺序统计量
1.第K顺序统计量概念 在一个由n个元素组成的集合中,第k个顺序统计量是该集合中第k小的元素.例如,最小值是第1顺序统计量,最大值是第n顺序统计量. 2.求Top K元素与求第K顺序统计量不同 Top ...
- javascript基础知识梳理-Number与String之间的互相转换【转】
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Data Binding MVVM 数据绑定 总结
示例代码:https://github.com/baiqiantao/DataBindingTest 参考:精通Android Data Binding Android Data Binding ...
- css表格单元格中的长文本的显示问题
自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大,挺让人头疼,下面介绍的是CSS如何实现换行的方法 对于div,p等块级元素 正常文字的换行(亚洲文字和非亚洲文字)元素拥 ...
- 转: telnet命令学习
1.每天一个linux命令(58):telnet命令 转自: http://www.cnblogs.com/peida/archive/2013/03/13/2956992.html telnet命令 ...
- C++的基本类型
- wireshark 的使用(filter的用法)
转自:http://blog.csdn.net/hanyuxinting/article/details/5558095 过滤器语法---------------------------------- ...
- IP综合
获取客户端IP的常用方法和注意事项: 进阶一:常用的方法,但是不够严谨. function getIP() { if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) ...
- C#.NET常见问题(FAQ)-Visual Studio VS如何显示行号
工具-选项,然后勾选"显示所有设置",然后在文本编辑器下面找到所有语言,勾选"行号"即可. 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: ...