NameValueCollection继承于抽象类NameObjectCollectionBase ,
NameObjectCollectionBase 的语法如下:
public abstract class NameObjectCollectionBase : ICollection, IEnumerable, ISerializable, IDeserializationCallback
NameValueCollection的语法如下:
public class NameValueCollection : NameObjectCollectionBase
下面列出NameValueCollection类的属性和方法:
下面让我们看看另外一个继承于NameObjectCollectionBase的类HttpApplicationState,
让我们对比一下NameValueCollection 和HttpApplicationState的不同之处和相似之处。
/*
Test For Jagged ArrayList From NameValueCollection
(The Upgraded Hashmap Type, Key-->Value1, Value2,ValueN)
[email protected]/10
Copy Right By K.Shi
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Collections;
namespace testNamevalueCollection
{
class testNamevalueCollection
{
staticvoid Main(string[] args)
{
NameValueCollection nvcn =new NameValueCollection();
nvcn.Add("FujiXerox","WorkShop");
nvcn.Add("FujiXerox","Teak");
nvcn.Add("FujiXerox","DocuWorks");
nvcn.Add("FujiXerox","PineHurst");
nvcn.Add("IBM","T42");
nvcn.Add("IBM","T43");
nvcn.Add("IBM","T60");
nvcn.Add("IBM","T60p");
nvcn.Add("IBM","T61");
nvcn.Add("IBM","T61p");
nvcn.Add("iPod","Classic");
nvcn.Add("iPod","nano");
nvcn.Add("iPod","nano2");
nvcn.Add("iPod","video");
nvcn.Add("iPod","photo");
nvcn.Add("iPod","touch");
nvcn.Add("iPod","phone");
nvcn.Add("ATH","ES7");
nvcn.Add("ATH","CK9");
/*
// Displays the values in the NameValueCollection in two different ways.
Console.WriteLine("Displays the elements using the AllKeys property and the Item (indexer) property:");
printKeyAndValues(nvcn);
Console.WriteLine("Displays the elements using GetKey and Get:");
printKeyAndValues2(nvcn);
// Gets a value either by index or by key.
Console.WriteLine("Index 1 contains the value: {0}.", nvcn[1]);
Console.WriteLine("Key /"Ipod/" has the value: {0}.", nvcn["iPod"]);
Console.WriteLine();
// Copies the values to a string array and displays the string array.
String[] myStrArr = new String[nvcn.Count];
nvcn.CopyTo(myStrArr, 0);
Console.WriteLine("The string array contains:");
foreach (String s in myStrArr)
Console.WriteLine("/t {0}", s);
Console.WriteLine();
// Searches for a key and deletes it.
nvcn.Remove("ATH");
Console.WriteLine("The collection contains the following elements after removing /"ATH/":");
printKeyAndValues(nvcn);
// Clears the entire collection.
nvcn.Clear();
Console.WriteLine("The collection contains the following elements after it is cleared:");
printKeyAndValues(nvcn);
*/
string[][] sa=newstring[nvcn.Count][];
//= new string[nvcn.Count,];
for (int m=0; m< nvcn.Count; m++)
{
Console.WriteLine(nvcn.GetValues(m).Length);
sa[m] =newstring[nvcn.GetValues(m).Length];
for (int n=0; n< nvcn.GetValues(m).Length; n++)
{
sa[m][n] = nvcn.GetValues(m)[n];
Console.WriteLine(sa[m][n]);
}
}
Console.WriteLine("/n/n");
for (int i=0; i< nvcn.Count; i++)
{
Console.Write("/nThe {0}: /n", i+1);
for (int j=0; j< nvcn.GetValues(i).Length; j++)
{
Console.WriteLine(sa[i][j]);
}
}
}
publicstaticvoid printKeyAndValues(NameValueCollection NVCN)
{
IEnumerator ietr = NVCN.GetEnumerator();
Console.WriteLine("/n#####################################"+
"/n# The Table Contains: ["+ NVCN.Count+"] Elements. #/n"
+"#####################################/n");
Console.WriteLine("/t KEY /t VALUE");
/*
foreach (String str in NVCN.AllKeys)
{
Console.WriteLine("/t {0} /t {1}", str, NVCN[str]);
}
*/
while (ietr.MoveNext())
{
Console.WriteLine("/t {0} /t {1}", ietr.Current, NVCN[ietr.Current.ToString()]);
}
Console.WriteLine();
}
publicstaticvoid printKeyAndValues2(NameValueCollection NVCN)
{
Console.WriteLine("/n#####################################"+
"/n# The Table Contains: ["+ NVCN.Count+"] Elements. #/n"
+"#####################################/n");
Console.WriteLine("/t [INDEX] /t KEY /t VALUE");
for (int i=0; i< NVCN.Count; i++)
Console.WriteLine("/t [{0}] /t {1} /t {2}", i, NVCN.GetKey(i), NVCN.Get(i));
Console.WriteLine();
}
}
}
运行结果:
6
Win3.1
Win98
Win2k
WinXP
Longhorn
WinVista
6
T42
T43
T60
T60p
T61
T61p
7
Classic
nano
nano2
video
photo
touch
phone
2
ES7
CK9
The 1:
Win3.1
Win98
Win2k
WinXP
Longhorn
WinVista
The 2:
T42
T43
T60
T60p
T61
T61p
The 3:
Classic
nano
nano2
video
photo
touch
phone
The 4:
ES7
CK9
続行するには何キーを押してください . . .