一个简单的例子

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;

publicclass SamplesNameValueCollection { publicstaticvoid 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 );

}

publicstaticvoid PrintKeysAndValues( NameValueCollection myCol )  {
     Console.WriteLine( "   KEY        VALUE" );
foreach ( String s in myCol.AllKeys )
        Console.WriteLine( "   {0,-10} {1}", s, myCol[s] );
     Console.WriteLine();
  }

publicstaticvoid 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

*/

转载于:https://blog.51cto.com/2347979/1196334

C# NameValueCollection相关推荐

  1. NameValueCollection详解

    1.NameValueCollection类集合是基于 NameObjectCollectionBase 类. 但与 NameObjectCollectionBase 不同,该类在一个键下存储多个字符 ...

  2. NameValueCollection类总结和一个例子源码

    1.NameValueCollection类集合是基于 NameObjectCollectionBase 类.但与 NameObjectCollectionBase 不同, 该类在一个键下存储多个字符 ...

  3. C#中NameValueCollection类用法详解

    C#中NameValueCollection类用法详解,1.NameValueCollection类集合是基于 NameObjectCollectionBase 类. 但与 NameObjectCol ...

  4. C# NameValueCollection集合 (转)

    C# NameValueCollection集合 (转) 1.NameValueCollection类集合是基于 NameObjectCollectionBase 类. 但与 NameObjectCo ...

  5. NBear.Mapping使用教程(5):实体对象与NameValueCollection,Dicitonary以及NBear.Mapping性能

    在上篇中,介绍了使用NBear.Mapping进行实体对象与ADO.NET对象的互转功能,这可能是我们开发过程中最常的一种映射了.除此之外,NBear.Mapping还支持实体对象与NameValue ...

  6. C#关于NameValueCollection,Dictionary,List Tuple比较

    我们在开发项目,根据实际需要,选择最适合的集合数据类型. NameValueCollection.Dictionary<string,string>,List<Tuple<st ...

  7. C#NameValueCollection的特殊测试,键不区分大小写

    NameValueCollection的键不区分大小写,当键重复时,只是将新增的值 合并到该键对应的值集合中. 测试程序如下: using System; using System.Collectio ...

  8. asp.net程序性能优化的七个方面

    asp.net程序性能优化的七个方面 一.数据库操作 1.用完马上关闭数据库连接 访问数据库资源需要创建连接.打开连接和关闭连接几个操作.这些过程需要多次与数据库交换信息以通过身份验证,比较耗费服务器 ...

  9. c oracle long raw,初次用Oracle,不会用Long Raw类型,请大家帮忙看看

    在存储过程中想传入一个二进制数组,保存到Oracle中的blob字段中 程序出现下面错误: ORA-01460: 未实现或无理的转换请求 Description: An unhandled excep ...

最新文章

  1. TermCriteria模板类
  2. java源码推荐_基于java的推荐系统实现源代码
  3. python的二维数组操作
  4. malloc()背后的实现原理——内存池
  5. Java后端WebSocket的Tomcat实现(转载)
  6. 深度学习基础(四)优化函数(梯度下降函数)
  7. Failed to resolve: android.arch.lifecycle:runtime:1.0.0,Failed to resolve: support-v4
  8. 苹果发布紧急公告:老设备11月3日前再不更新就要停止服务啦
  9. Android学习--写一个发送短信的apk,注意布局文件的处理过程!!!
  10. oracle启动限制模式
  11. 跨域的另一种解决方案——CORS(Cross-Origin Resource Sharing)跨域资源共享
  12. 1.4 高并发之线程和进程
  13. php常见错误和解决办法
  14. 【对讲机的那点事】维修对讲机你会拆卸电路板上的集成电路块吗?
  15. Mysql Fabric实现学习笔记
  16. 如何使用dos启动mysql数据库_如何使用dos命令启动停止mysql数据库?
  17. hbase数据库scan操作_HBase Scan 使用
  18. html5 图片上传进度条,html5异步上传图片显示上传文件进度条
  19. 【DL-安装遇错】解决出现 pip‘s dependency resolver does not currently take into account all the packages that..
  20. css3图片放大溢出,用canvas调整图像大小 - css溢出问题

热门文章

  1. MyEclipse设置代码自动补全,及取消空格和‘=’补全
  2. 舵机控制激光头(51单片机)
  3. VMware15设置快照回到指定时间的状态
  4. html + css + js 实现简易计算器
  5. cpython python 区别面试_python基础教程之千万不要错过这几道Python面试题
  6. SpringDataRedis的简单案例使用
  7. Alpha冲刺 - (4/10)
  8. 【EMV L2】数据元格式 对齐方式
  9. 思科交换机的初始配置(使用telnet登录)
  10. mybatis中association和collection的column传入多个参数值