C# 中的集合类是什么?
csharpprogrammingserver side programming更新于 2025/9/26 8:37:17
集合类有多种用途,例如为元素动态分配内存、根据索引访问项目列表等。
以下是 Collections − 中的类
| Sr.No | 类 & 描述 & |
|---|---|
| 1 | ArrayList 它表示可以单独索引的对象的有序集合。 |
| 2 | Hashtable 它使用键来访问集合中的元素。 |
| 3 | SortedList 它使用键和索引来访问列表中的项目。 |
| 4 | Stack 它表示后进先出的对象集合。 |
| 5 | Queue 它表示先进先出的对象。 |
| 6 | BitArray 它表示使用值 1 和 0 的二进制表示数组。 |
让我们看一个 C# 中 BitArray 类的示例 −
示例
using System;
using System.Collections;
namespace CollectionsApplication {
class Program {
static void Main(string[] args) {
//创建两个大小为 8 的位数组
BitArray ba1 = new BitArray(8);
BitArray ba2 = new BitArray(8);
byte[] a = { 60 };
byte[] b = { 13 };
//将值 60 和 13 存储到位数组中
ba1 = new BitArray(a);
ba2 = new BitArray(b);
//ba1 的内容
Console.WriteLine("位数组 ba1: 60");
for (int i = 0; i < ba1.Count; i++) {
Console.Write("{0, -6} ", ba1[i]);
}
Console.WriteLine();
//ba2 的内容
Console.WriteLine("位数组 ba2: 13");
for (int i = 0; i < ba2.Count; i++) {
Console.Write("{0, -6} ", ba2[i]);
}
Console.WriteLine();
BitArray ba3 = new BitArray(8);
ba3 = ba1.And(ba2);
//ba3的内容
Console.WriteLine("Bit array ba3 after AND operation: 12");
for (int i = 0; i < ba3.Count; i++) {
Console.Write("{0, -6} ", ba3[i]);
}
Console.WriteLine();
ba3 = ba1.Or(ba2);
//ba3的内容
Console.WriteLine("Bit array ba3 after OR operation: 61");
for (int i = 0; i < ba3.Count; i++) {
Console.Write("{0, -6} ", ba3[i]);
}
Console.WriteLine();
Console.ReadKey();
}
}
}
输出
Bit array ba1: 60 False False True True True True False False Bit array ba2: 13 True False True True False False False False Bit array ba3 after AND operation: 12 False False True True False False False False Bit array ba3 after OR operation: 61 True False True True False False False False

