什么是C#中的泛型集合?
C#中的通用集合包括<List>,<SortedList>等。
列表
List<T>是通用集合,而ArrayList是非通用集合。
让我们来看一个例子。在这里,我们在列表中有六个元素-
示例
using System;
using System.Collections.Generic;
class Program {
static void Main() {
//初始化集合
List myList = new List() {
"one",
"two",
"three",
"four",
"five",
"six"
};
Console.WriteLine(myList.Count);
}
}输出结果
6
SortedList
排序列表是数组和哈希表的组合。它包含可以使用键或索引访问的项目列表。
让我们来看一个例子。在这里,我们在SortedList中有四个元素-
示例
using System;
using System.Collections;
namespace CollectionsApplication {
class Program {
static void Main(string[] args) {
SortedList sl = new SortedList();
sl.Add("001", "Tim");
sl.Add("002", "Steve");
sl.Add("003", "Bill");
sl.Add("004", "Tom");
if (sl.ContainsValue("Bill")) {
Console.WriteLine("This name is already in the list");
} else {
sl.Add("005", "James");
}
ICollection key = sl.Keys;
foreach (string k in key) {
Console.WriteLine(k + ": " + sl[k]);
}
}
}
}输出结果
This name is already in the list 001: Tim 002: Steve 003: Bill 004: Tom
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿