集合是C#中一个重要的概念,下面给出一个集合拷贝的例子。
using System;
using System.Collections;
using System.Collections.Specialized;
namespace 集合的拷贝
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
[STAThread]
static void Main(string[] args)
{
NameValueCollection namedVColl=new NameValueCollection();
namedVColl.Add("晓华","13510532686");
namedVColl.Add("晓华","62658888");
namedVColl.Add("小杨","1361030486");
namedVColl.Add("小杨","62293218");
foreach(string key in namedVColl.Keys)
Console.WriteLine("姓名={0},电话={1}",key,namedVColl[key]);
Console.WriteLine("拷贝后获得的命名值集合");
string[] arr=new string[namedVColl.Count];
namedVColl.CopyTo(arr,0);
for(int i=arr.GetLowerBound(0);i<arr.GetUpperBound(0);i++)
{
Console.WriteLine("姓名={0},电话={1}",namedVColl.AllKeys[i],arr[i]);
}
Hashtable hhtable=new Hashtable();
hhtable["晓华"]="北京";
hhtable["小杨"]="南京";
Console.WriteLine("克隆前哈希表");
foreach(object key in hhtable.Keys)
Console.WriteLine("姓名={0},地址={1}",key,hhtable[key]);
Hashtable hhCloned=(Hashtable)hhtable.Clone();
hhtable["晓华"]="上海";
Console.WriteLine("克隆修改初始化哈希表");
foreach(object key in hhtable.Keys)
Console.WriteLine("姓名={0},地址={1}",key,hhtable[key]);
Console.WriteLine("修改后初始化哈希表后的克隆哈萨克希表");
foreach(object key in hhCloned.Keys)
{
Console.WriteLine("姓名={0},地址={1}",key,hhCloned[key]);
}
Console.Read();
}
}
}
新闻热点
疑难解答