1.NameValueCollection类集合是基于 NameObjectCollectionBase 类。
但与 NameObjectCollectionBase 不同,该类在一个键下存储多个字符串值(就是键相同,值就连接起来如下例子)。该类可用于标头、查询字符串和窗体数据。
每个元素都是一个键/值对。NameValueCollection 的容量是 NameValueCollection 可以保存的元素数。
NameValueCollection 的默认初始容量为零。随着向 NameValueCollection 中添加元素,容量通过重新分配按需自动增加。
如下例子:

NameValueCollection myCol = new NameValueCollection();
myCol.Add("red", "rojo");//如果键值red相同结果合并 rojo,rouge
myCol.Add("green", "verde");
myCol.Add("blue", "azul");
myCol.Add("red", "rouge");

2.NameValueCollection与Hashtable的区别
a.引用区别

hashtable:using System.Collections;

NameValueCollection:using System.Collections.Specialized;

b.键是否重复

NameValueCollection:允许重复.

HashTable是键-值集合,但键不能出现重复.

Hashtable ht = new Hashtable();
ht.Add("key","value");
ht.Add("key", "value1"); //出错
ht["key"] = "value1"; //正确

3.初始化NameValueCollection
初始化NameValueCollection需引用using System.Collections.Specialized;
完整例子源码:

using System; using System.Collections;
using System.Collections.Specialized;

namespace SamplesNameValueCollection
{
class Program
{

public static void Main()
{
//初始化NameValueCollection需引用using System.Collections.Specialized;
NameValueCollection myCol = new NameValueCollection();
myCol.Add("red", "rojo");//如果键值red相同结果合并 rojo,rouge
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]);//索引1的值
Console.WriteLine("Key \"red\" has the value {0}.", myCol["red"]);//键为red的对应值rouge
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();

//查找green键值然后删除
myCol.Remove("green");
Console.WriteLine("The collection contains the following elements after removing \"green\":");
PrintKeysAndValues(myCol);

//清空集合
myCol.Clear();
Console.WriteLine("The collection contains the following elements after it is cleared:");
PrintKeysAndValues(myCol);

}
//显示键,值
public static void PrintKeysAndValues(NameValueCollection myCol)
{
IEnumerator myEnumerator = myCol.GetEnumerator();
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();
}
}

}
4.NameValueCollection遍历
与Hashtable相似:

NameValueCollection myCol = new NameValueCollection();
myCol.Add("red", "rojo");//如果键值red相同结果合并 rojo,rouge
myCol.Add("green", "verde");
myCol.Add("blue", "azul");
myCol["red"] = "dd";

foreach (string key in myCol.Keys)
{
Console.WriteLine("{0}:{1}", key, myCol[key]);
}
Console.ReadLine();

NameValueCollection类集合的更多相关文章

  1. NameValueCollection类读取配置信息

    C#中的NameValueCollection类读取配置信息,大家可以参考下.   我首先介绍配置文件中的写法: 1.在VS2015中的工程下建立一个控制台应用程序,其config文件默认名称为App ...

  2. 第八节:详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架

    前言 大家好,给大家带来详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架的概述,希望你们喜欢 JAVA 异常 try...catch...finally结构的使用方法 class Tes ...

  3. 多个module实体类集合打一个jar包并上传至远程库

    本章内容主要分享多个module中的实体类集合生成到一个jar包中,并且发布到远程库:这里采用maven-assembly-plugin插件的功能来操作打包,内容不长却贴近实战切值得拥有,主要节点内容 ...

  4. Java开发工具类集合

    Java开发工具类集合 01.MD5加密工具类 import java.security.MessageDigest; import java.security.NoSuchAlgorithmExce ...

  5. NameValueCollection类

    最近在研究HttpRequest类,发现里面的很多属性都返回一个NameValueCollection对象,今天再来了解一下这个神秘的对象. 随便写了个例子,发现跟HashTable类似.但是这个东西 ...

  6. MVC控制器传递多个实体类集合到视图的方案总结

    MVC控制器向视图传递数据包含多个实体类的解决方案有很多,这里主要针对视图模型.动态模型以及Tuple三种方法进行一些总结与记录. 基础集合类:TableA namespace ViewModelSt ...

  7. [转]Java常用工具类集合

    转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...

  8. 创建泛类集合List以及数组转集合,集合转数组的应用

    List<int> list = new List<int>(); list.Add(); list.Add(); list.Add(); list.AddRange(, , ...

  9. 用list<类>集合接收一个网址返回的一个类的集合的XML

    JavaScriptSerializer serializer = new JavaScriptSerializer(); string json = Share.Helper.HttpRequest ...

随机推荐

  1. Swift:如何判断一个对象是否是某个类(或其子类)的实例

    在OC中我们直接可以用如下方法即可 [obj  isKindOfClass:[obj class]]; 在Swift中,并没有 .class 属性或者方法, 便可以用如下方法 class Person ...

  2. algo_预备

    章 C + +程序设计 大家好!现在我们将要开始一个穿越" 数据结构.算法和程序" 这个抽象世界的特殊旅程,以解决现实生活中的许多难题.在程序开发过程中通常需要做到如下两点:一是高 ...

  3. Rsync 详细配置说明

    rsync是类unix系统下的数据镜像备份工具. 它的特性如下:可以镜像保存整个目录树和文件系统.可以很容易做到保持原来文件的权限.时间.软硬链接等等.无须特殊权限即可安装.快速:第一次同步时 rsy ...

  4. hdu1251在词典里统计前缀出现的个数

    banana band bee absolute acm    ba b band abc #include<iostream> using namespace std; //数据结构 s ...

  5. ubuntu下搭建lamp

    一.使用apt-get方式为Ubuntu安装PHP+MYSQL+Apache   分别执行如下命令:   (1)安装MYSQL   sudo apt-get install mysql-server ...

  6. git merge之squash

    看CM源码时,发现历史记录里有很多squash,于是google了解了一下. Git相对于CVS和SVN的一大好处就是merge非常方便,只要指出branch的名字就好了,如: 1 2 3 4 5 $ ...

  7. Sharepoint 2013列表视图和字段权限扩展插件(免费下载)!

    记得2014年春节期间,有博客园的网友通过QQ向我咨询Sharepoint 2013列表视图和字段权限扩展,因为之前他看到我博客介绍Sharepoint 2010列表视图和字段的权限控制扩展使用,问有 ...

  8. 审核流(2)流程设计-SNF.WorkFlow功能使用说明--SNF快速开发平台3.1

    流程设计 图形化的流程设计,更方便.直观 1.打开“流程设计“程序,如上.点击”新建“如下: 2.红色部分为必填项,审批对象是选择要审批的程序菜单,单据名称是在审核流流转时用于提示的单据名称,还要选择 ...

  9. 谷歌正式发布Google APIs Client Library for .NET

    好消息,特大好消息! 英文原文:Google API library for .NET paves the way for Google services on Windows phone 本月 17 ...

  10. codeforces Fedor and New Game

    #include<iostream> #include<stack> #include<cstring> #include<cstdio> #inclu ...