Skip to Content
C# 12 核心要点
book

C# 12 核心要点

by Joseph Albahari
May 2025
Intermediate to advanced
1086 pages
14h 54m
Chinese
O'Reilly Media, Inc.
Content preview from C# 12 核心要点

第 7 章. 收藏

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

.NET 为存储和管理对象集合提供了一套标准类型。这些类型包括可调整大小的列表、链接列表、已排序和未排序字典以及数组。在这些集合中,只有数组是 C# 语言的一部分;其余的集合只是类,您可以像实例化其他类一样实例化它们。

我们可以将 .NET BCL 中的集合类型分为以下几类:

  • 定义标准采集协议的接口

  • 随时可用的集合类(列表、字典等)

  • 用于编写应用程序特定集合的基类

本章将逐一介绍这些类别,另有一节介绍用于确定元素相等和顺序的类型。

集合命名空间如下

命名空间 包含
System.Collections 非通用集合类和接口
System.Collections.Specialized 强类型非通用集合类
System.Collections.Generic 通用收集类和接口
System.Collections.ObjectModel 自定义集合的代理和基础
System.Collections.Concurrent 线程安全集合(参见第 23 章)

枚举

在计算领域,有许多不同种类的集合,从简单的数据结构(如数组或链表)到更复杂的数据结构(如红/黑树和哈希表),不一而足。虽然这些数据结构的内部实现和外部特征千差万别,但遍历集合内容的能力几乎是一种普遍需求。.NET BCL 通过一对接口(IEnumerableIEnumerator 及其通用对应接口)来支持这种需求,这些接口允许不同的数据结构公开通用的遍历 API。这些接口是图 7-1 所示的更大集合接口集的一部分。

Collection interfaces
图 7-1. 采集界面

IEnumerable 和 IEnumerator

IEnumerator 接口定义了基本的底层协议,通过该协议,可以以只向前的方式遍历或枚举集合中的元素。其声明如下

public interface IEnumerator
{
  bool MoveNext();
  object Current { get; }
  void Reset();
}

MoveNext 将当前元素或 "光标 "推进到下一个位置,如果集合中没有更多元素,则返回 。 返回当前位置的元素(通常从 转换为更具体的类型)。必须在检索第一个元素之前调用 ,以允许出现空集合。 的存在主要是为了实现组件对象模型(COM)的互操作性;通常避免直接调用它,因为它并不被普遍支持(而且没有必要,因为通常实例化一个新的枚举器也很容易)。false Current object MoveNext Reset Reset

集合通常不实现枚举器,而是通过接口IEnumerable提供枚举器:

public interface IEnumerable
{
  IEnumerator GetEnumerator();
}

通过定义单个方法来重调枚举器,IEnumerable 提供了灵活性,可以将迭代逻辑外包给其他类。此外,这还意味着多个消费者可以同时枚举集合,而不会相互干扰。你可以将IEnumerable 视为 "IEnumeratorProvider",它是集合类实现的最基本接口。

下面的示例说明了IEnumerableIEnumerator ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Programming C# 12

Programming C# 12

Ian Griffiths
C# 12 in a Nutshell

C# 12 in a Nutshell

Joseph Albahari
C# 6 for Programmers, Sixth Edition

C# 6 for Programmers, Sixth Edition

Paul Deitel, Harvey Deitel
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 9798341657038