在C#中获取SortedList对象中的值
要获取SortedList对象中的值,代码如下-
示例
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList list = new SortedList();
list.Add(1, "One");
list.Add(2, "Two");
list.Add(3, "Three");
list.Add(4, "Four");
list.Add(5, "Five");
ICollection col1 = list.Values;
Console.WriteLine("Values...");
foreach(string s in col1)
Console.WriteLine(s);
ICollection col2 = list.Keys;
Console.WriteLine("Keys...");
foreach(int s in col2)
Console.WriteLine(s);
}
}输出结果
这将产生以下输出-
Values... One Two Three Four Five Keys... 1 2 3 4 5
示例
让我们看另一个例子-
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");
ICollection col1 = list.Values;
Console.WriteLine("Values...");
foreach(string s in col1)
Console.WriteLine(s);
ICollection col2 = list.Keys;
Console.WriteLine("\nKeys...");
foreach(string s in col2)
Console.WriteLine(s);
}
}输出结果
这将产生以下输出-
Values... Jacob Sam Tom John Tim Mark Gary Nathan Shaun David Keys... A B C D E F G H I J
热门推荐
3 带清的简短祝福语
10 上岸离职祝福语大全简短
11 新年祝福语长篇文案简短
12 许愿星姐妹祝福语简短
13 给叔叔虎年祝福语简短
14 女生成年祝福语简短
15 祝寿时祝福语简短精辟
16 空军大学开学祝福语简短
17 情人表白祝福语简短大气
18 迎接朋友蛋糕祝福语简短