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 核心要点

第 23 章 跨度<T>和内存<T 跨度<T> 和内存<T

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

Span<T> 和 结构是数组、字符串或任何连续的受管或非受管内存块的底层外观。它们的主要用途是帮助进行某些类型的微优化,尤其是编写Memory<T> 低分配代码,最大限度地减少受管内存的分配(从而减轻垃圾回收器的负担),而不必为不同类型的输入重复编写代码。此外,它们还能进行切片处理--在不创建副本的情况下处理数组、字符串或内存块的一部分。

Span<T> 和 在性能热点中特别有用,例如 ASP.NET Core 处理流水线或为对象数据库提供服务的 JSON 分析器。Memory<T>

备注

如果您在应用程序接口中遇到这些类型,但不需要或不关心其潜在的性能优势,您可以按以下方法轻松处理它们:

  • 当调用一个期望使用Span<T>,ReadOnlySpan<T>,Memory<T>, 或ReadOnlyMemory<T> 的方法时,请输入一个数组,即T[] 。(这要归功于隐式转换操作符)。

  • 要将 span/memory 转换数组,请调用ToArray 方法。如果TcharToString 将把 span/memory 转换为字符串。

从 C# 12 开始,您还可以使用集合初始化程序来创建跨。

具体来说,Span<T> 做了两件事:

  • 它为托管数组、字符串和指针支持的内存提供了类似数组的通用接口。这样,你就可以自由地使用堆栈分配和非托管内存来避免垃圾回收,而无需重复代码或处理指针。

  • 它允许 "切片":在不复制的情况下,公开跨度中可重复使用的分段。

备注

Span<T> 只包含两个字段,一个指针和一个长度。因此,它只能表示连续的内存块。(如果需要处理非连续内存, 类可用作链表)。ReadOnlySequence<T>

由于Span<T> 可以封装堆栈分配的内存,因此存储或传递实例的方式受到了限制(部分原因是Span<T> 是一个ref 结构)。Memory<T> 作为一个 span,没有这些限制,但它不能封装堆栈分配的内存。Memory<T> 仍然提供了切分的好处。

每个结构体都有一个只读对应结构体(ReadOnlySpan<T>ReadOnly​Me⁠mory<T> )。只读对应结构不仅可以防止无意中的更改,还可以允许编译器和运行时进行更多的自由优化,从而进一步提高性能。

.NET 本身(和 ASP.NET Core)使用这些类型来提高 I/O、Network+、字符串处理和 JSON 解析的效率。

备注

Span<T> 和 执行数组切分的能力,使得旧的 类变得多余。为了帮助过渡,我们提供了从 到所有 span/memory 结构的隐式转换操作符,以及从Memory<T> ArraySegment<T> ArraySegment<T> Memory<T>ReadOnlyMemory<T>ArraySegment<T> 的隐式转换操作符。

跨度和切片

如图 23-1 所示,与数组不同,跨度可以很容易地进行切分,以表示同一基础数据的不同子部分。

举个实际的例子,假设您正在编写一个方法,对一个整数数组求和。微优化的实现方式是避免使用 LINQ,而使用foreach 循环:

int Sum (int[] numbers) { int total = 0; foreach (int i in numbers) total += i; return total; ...
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