与数组最大的区别,集合的长度(元素的个数)可以动态改变,可以向集合添加、删除元素 。
1.集合分类
1.1System.Collections.Generic 类
当集合中的所有元素都具有相同的数据类型时,泛型集合会非常有用。
类 | 说明 | 元素的顺序 | 元素重复的值 |
---|---|---|---|
Dictionary<TKey,TValue> | 键/值对集合 | 无序(按键访问值) | 允许 |
HashSet<T> | 哈希集 | 无序(按键访问键) | 不允许 |
List<T> | 索引列表 | 有序(按索引访问值) | 允许 |
Queue<T> | 先进先出 (FIFO) 集合 | 留白 | 留白 |
SortedList<TKey,TValue> | 基于相关联的 IComparer<T> 实现按键进行排序的键/值对的集合 | 留白 | 留白 |
Stack<T> | 后进先出 (LIFO) 集合 | 留白 | 留白 |
1.2System.Collections.Concurrent 类
只要多个线程同时访问集合,就应使用 System.Collections.Concurrent 命名空间中的类,而不是 System.Collections.Generic 和 System.Collections 命名空间中的相应类型。
类 | 说明 |
Blocking | 为实现 IProducerConsumerCollection<T> 的线程安全集合提供阻塞和限制功能。 |
Concurrent | 表示对象的线程安全的无序集合。 |
Concurrent | 表示可由多个线程同时访问的键/值对的线程安全集合。 |
Concurrent | 表示线程安全的先进先出 (FIFO) 集合。 |
Concurrent | 表示线程安全的后进先出 (LIFO) 集合。 |
Orderable | 表示将可排序数据源拆分为多个分区的特定方式。 |
Partitioner | 提供针对数组、列表和可枚举项的常见分区策略。 |
Partitioner<TSource> | 表示将数据源拆分为多个分区的特定方式。 |
1.3System.Collections 类
只要可能,则应使用 System.Collections.Generic 命名空间或 System.Collections.Concurrent 命名空间中的泛型集合,而不是 System.Collections
命名空间中的旧类型。
类 | 说明 |
---|---|
ArrayList | 表示对象的数组,这些对象的大小会根据需要动态增加。 |
Hashtable | 表示根据键的哈希代码进行组织的键/值对的集合。 |
Queue | 表示对象的先进先出 (FIFO) 集合。 |
Stack | 表示对象的后进先出 (LIFO) 集合。 |
1.4专用集合
System.Span<T>专用集合:用于访问堆栈帧上的连续内存。
System.Memory<T>专用集合:用于访问托管堆上的连续内存。
1.5创建自定义集合
可以通过实现 System.Collections.IEnumerable 或 System.Collections.Generic.IEnumerable<T>接口来定义集合。
2.索引集合
//使用构造函数创建索引集合的对象。
List<string> c = new List<string>();
//添加元素到结尾处
c.Add("chinook");
c.Add("coho");
c.Add("pink");
c.Add("sockeye");
//删除元素
c.Remove("coho");
//[]可以查询和更改已有的元素,不可以添加元素
b = c[0];
c[0] = value;
//使用构造函数和集合初始值设定项创建索引集合的对象。
List<string> c = new List<string>() { "chinook", "coho", "pink", "sockeye" };
//注意:索引集合不支持类似数组的短语法。
List<string> c = { "chinook", "coho", "pink", "sockeye" };
3.键值集合
//使用构造函数创建键值集合的对象。
Dictionary<string, string> c = new Dictionary<string, string>();
//添加元素
c.Add("txt", "notepad.exe");
c.Add("bmp", "paint.exe");
c.Add("dib", "paint.exe");
c.Add("rtf", "wordpad.exe");
//删除元素
c.Remove("bmp");
//[]可以查询和更改已有的元素,还可以添加元素
b = c["key"];
c["key"] = value;
//使用构造函数和集合初始值设定项创建键值集合的对象。
var numbers = new Dictionary<int, string>()
{
[7] = "seven",
[9] = "nine",
[13] = "thirteen"
};
//C#6.0之前语法
var moreNumbers = new Dictionary<int, string>
{
{19, "nineteen" },
{23, "twenty-three" },
{42, "forty-two" }
};
//注意:键值集合不支持类似数组的短语法。
var numbers = { [7] = "seven", [9] = "nine", [13] = "thirteen" };
4.集合初始值设定项(Collection Initializer)
使用 集合初始值设定项(Collection Initializer),可以在new
运算符创建集合类型的对象时初始化元素,此集合类型是 实现IEnumerable且有Add()
方法(作为实例方法或扩展方法并适合的签名) 集合类型。
集合初始值设定项(Collection Initializer)内的元素可以是简单的值、表达式、方法、属性、索引器、对象初始值设定项。
//简单的值
List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//表达式、方法
List<int> digits2 = new List<int> { 0 + 1, 12 % 3, MakeInt() };
//属性
List<int> zs = new() { Capacity = 20_000 };
//对象初始值设定项
//Cat类具有string Name和 int Age两个属性
//语法一
List<Cat> cats = new List<Cat>
{
{ new Cat { Name = "Sylvester", Age=8 }},
{ new Cat { Name = "Whiskers", Age=2 }},
{ new Cat { Name = "Sasha", Age=14 }}
};
//语法二(省略圆括号)
List<Cat> cats = new List<Cat>
{
new Cat{ Name = "Sylvester", Age=8 },
new Cat{ Name = "Whiskers", Age=2 },
new Cat{ Name = "Sasha", Age=14 }
};
//对象初始值设定项
//CatOwner具有IList<cat>只读集合属性
public class CatOwner
{
public IList<Cat> Cats { get; } = new List<Cat>();
}
//由于无法为属性分配新列表,所以可以使用集合初始值设定项语法解决问题
CatOwner owner = new CatOwner
{
Cats = new List<Cat>
{
new Cat{ Name = "Sylvester", Age=8 },
new Cat{ Name = "Whiskers", Age=2 },
new Cat{ Name = "Sasha", Age=14 }
}
};
//省略new List<Cat>
CatOwner owner = new CatOwner
{
Cats =
{
new Cat{ Name = "Sylvester", Age=8 },
new Cat{ Name = "Whiskers", Age=2 },
new Cat{ Name = "Sasha", Age=14 }
}
};
5.获取集合长度
Count属性用于获取集合的长度或键/值对的数目。
c.Count
原创文章,作者:huoxiaoqiang,如若转载,请注明出处:https://www.huoxiaoqiang.com/csharp/csharplang/8042.html