如何在C#中创建SortedList对象的浅表副本?
要创建SortedList对象的浅表副本,代码如下-
示例
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args){
SortedList list = new SortedList();
list.Add("A", "Jacob");
list.Add("B", "Sam");
list.Add("C", "Tom");
list.Add("D", "John");
list.Add("E", "Tim");
list.Add("F", "Mark");
list.Add("G", "Gary");
list.Add("H", "Nathan");
list.Add("I", "Shaun");
list.Add("J", "David");
Console.WriteLine("SortedList elements...");
foreach(DictionaryEntry d in list){
Console.WriteLine(d.Key + " " + d.Value);
}
ICollection col1 = list.Values;
Console.WriteLine("\nValues...");
foreach(string s in col1)
Console.WriteLine(s);
ICollection col2 = list.Keys;
Console.WriteLine("\nKeys...");
foreach(string s in col2)
Console.WriteLine(s);
SortedList list2 = (SortedList)list.Clone();
Console.WriteLine("\nResultant SortedList...cloned from above list");
foreach(DictionaryEntry d in list){
Console.WriteLine(d.Key + " " + d.Value);
}
}
}输出结果
这将产生以下输出-
SortedList elements... A Jacob B Sam C Tom D John E Tim F Mark G Gary H Nathan I Shaun J David Values... Jacob Sam Tom John Tim Mark Gary Nathan Shaun David Keys... A B C D E F G H I J Resultant SortedList...cloned from above list A Jacob B Sam C Tom D John E Tim F Mark G Gary H Nathan I Shaun J David
示例
现在让我们来看另一个示例-
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args){
SortedList list = new SortedList();
list.Add("One", "IT");
list.Add("Two ", "Operations");
list.Add("Three", "Marketing");
list.Add("Four", "Purchase");
list.Add("Five", "Sales");
list.Add("Six", "Finance");
Console.WriteLine("SortedList elements...");
foreach(DictionaryEntry d in list){
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nList of values...SortedList");
IList col = list.GetValueList();
foreach(string res in col) {
Console.WriteLine(res);
}
Console.WriteLine("\nSortedList is read-only? = "+list.IsReadOnly);
SortedList list2 = (SortedList)list.Clone();
Console.WriteLine("\nResultant SortedList...cloned from above list");
foreach(DictionaryEntry d in list){
Console.WriteLine(d.Key + " " + d.Value);
}
}
}输出结果
这将产生以下输出-
SortedList elements... Five Sales Four Purchase One IT Six Finance Three Marketing Two Operations List of values...SortedList Sales Purchase IT Finance Marketing Operations SortedList is read-only? = False Resultant SortedList...cloned from above list Five Sales Four Purchase One IT Six Finance Three Marketing Two Operations
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短