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;

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


*/




本文转自 jirigala 51CTO博客,原文链接:http://blog.51cto.com/2347979/1196334,如需转载请自行联系原作者

相关文章
|
C#
C#中的NameValueCollection简介
NameValueCollection继承自NameObjectCollectionBase,并且和一般的键值对不同的是,它支持集合中出现相同的Key。 引用:using System.Collections.
1931 0
|
C# 数据安全/隐私保护
|
开发框架 前端开发 .NET
C#编程与Web开发
【4月更文挑战第21天】本文探讨了C#在Web开发中的应用,包括使用ASP.NET框架、MVC模式、Web API和Entity Framework。C#作为.NET框架的主要语言,结合这些工具,能创建动态、高效的Web应用。实际案例涉及企业级应用、电子商务和社交媒体平台。尽管面临竞争和挑战,但C#在Web开发领域的前景将持续拓展。
573 3
|
4月前
|
XML 前端开发 C#
C#编程实践:解析HTML文档并执行元素匹配
通过上述步骤,可以在C#中有效地解析HTML文档并执行元素匹配。HtmlAgilityPack提供了一个强大而灵活的工具集,可以处理各种HTML解析任务。
271 19
|
5月前
|
监控 算法 C#
C#与Halcon联合编程实现鼠标控制图像缩放、拖动及ROI绘制
C#与Halcon联合编程实现鼠标控制图像缩放、拖动及ROI绘制
956 0
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
300 3
|
存储 安全 编译器
学懂C#编程:属性(Property)的概念定义及使用详解
通过深入理解和使用C#的属性,可以编写更清晰、简洁和高效的代码,为开发高质量的应用程序奠定基础。
1017 12
|
设计模式 C# 图形学
Unity 游戏引擎 C# 编程:一分钟浅谈
本文介绍了在 Unity 游戏开发中使用 C# 的基础知识和常见问题。从 `MonoBehavior` 类的基础用法,到变量和属性的管理,再到空引用异常、资源管理和性能优化等常见问题的解决方法。文章还探讨了单例模式、事件系统和数据持久化等高级话题,旨在帮助开发者避免常见错误,提升游戏开发效率。
567 4
|
安全 程序员 编译器
C#一分钟浅谈:泛型编程基础
在现代软件开发中,泛型编程是一项关键技能,它使开发者能够编写类型安全且可重用的代码。C# 自 2.0 版本起支持泛型编程,本文将从基础概念入手,逐步深入探讨 C# 中的泛型,并通过具体实例帮助理解常见问题及其解决方法。泛型通过类型参数替代具体类型,提高了代码复用性和类型安全性,减少了运行时性能开销。文章详细介绍了如何定义泛型类和方法,并讨论了常见的易错点及解决方案,帮助读者更好地掌握这一技术。
270 11