C# 8.0 中的新增功能What's new in C# 8.0

C# 8.0 向 C# 语言添加了以下功能和增强功能:C# 8.0 adds the following features and enhancements to the C# language:

“.NET Core 3.x”和“.NET Standard 2.1”支持 C# 8.0 。C# 8.0 is supported on .NET Core 3.x and .NET Standard 2.1. 有关详细信息,请参阅 C# 语言版本控制For more information, see C# language versioning.

本文的剩余部分将简要介绍这些功能。The remainder of this article briefly describes these features. 如果有详细讲解的文章,则将提供指向这些教程和概述的链接。Where in-depth articles are available, links to those tutorials and overviews are provided. 可以使用 dotnet try 全局工具在环境中浏览这些功能:You can explore these features in your environment using the dotnet try global tool:

  1. 安装 dotnet-try 全局工具。Install the dotnet-try global tool.
  2. 克隆 dotnet/try-samples 存储库。Clone the dotnet/try-samples repository.
  3. 将当前目录设置为 try-samples 存储库的 csharp8 子目录 。Set the current directory to the csharp8 subdirectory for the try-samples repository.
  4. 运行 dotnet tryRun dotnet try.

Readonly 成员Readonly members

可将 readonly 修饰符应用于结构的成员。You can apply the readonly modifier to members of a struct. 它指示该成员不会修改状态。It indicates that the member doesn't modify state. 这比将 readonly 修饰符应用于 struct 声明更精细。It's more granular than applying the readonly modifier to a struct declaration. 请考虑以下可变结构:Consider the following mutable struct:

public struct Point
{
    public double X { get; set; }
    public double Y { get; set; }
    public double Distance => Math.Sqrt(X * X + Y * Y);

    public override string ToString() =>
        $"({X}, {Y}) is {Distance} from the origin";
}

与大多数结构一样,ToString() 方法不会修改状态。Like most structs, the ToString() method doesn't modify state. 可以通过将 readonly 修饰符添加到 ToString() 的声明来对此进行指示:You could indicate that by adding the readonly modifier to the declaration of ToString():

public readonly override string ToString() =>
    $"({X}, {Y}) is {Distance} from the origin";

上述更改会生成编译器警告,因为 ToString 访问未标记为 readonlyDistance 属性:The preceding change generates a compiler warning, because ToString accesses the Distance property, which isn't marked readonly:

warning CS8656: Call to non-readonly member 'Point.Distance.get' from a 'readonly' member results in an implicit copy of 'this'

需要创建防御性副本时,编译器会发出警告。The compiler warns you when it needs to create a defensive copy. Distance 属性不会更改状态,因此可以通过将 readonly 修饰符添加到声明来修复此警告:The Distance property doesn't change state, so you can fix this warning by adding the readonly modifier to the declaration:

public readonly double Distance => Math.Sqrt(X * X + Y * Y);

请注意,readonly 修饰符对于只读属性是必需的。Notice that the readonly modifier is necessary on a read-only property. 编译器会假设 get 访问器可以修改状态;必须显式声明 readonlyThe compiler doesn't assume get accessors don't modify state; you must declare readonly explicitly. 自动实现的属性是一个例外;编译器将所有自动实现的 Getter 视为 readonly,因此,此处无需向 XY 属性添加 readonly 修饰符。Auto-implemented properties are an exception; the compiler will treat all auto-implemented getters as readonly, so here there's no need to add the readonly modifier to the X and Y properties.

编译器确实会强制执行 readonly 成员不修改状态的规则。The compiler does enforce the rule that readonly members don't modify state. 除非删除 readonly 修饰符,否则不会编译以下方法:The following method won't compile unless you remove the readonly modifier:

public readonly void Translate(int xOffset, int yOffset)
{
    X += xOffset;
    Y += yOffset;
}

通过此功能,可以指定设计意图,使编译器可以强制执行该意图,并基于该意图进行优化。This feature lets you specify your design intent so the compiler can enforce it, and make optimizations based on that intent. 有关详细信息,请参阅有关 readonly 的语言参考文章中的 readonly 成员。You can learn more about readonly members in the language reference article on readonly.

默认接口方法Default interface methods

现在可以将成员添加到接口,并为这些成员提供实现。You can now add members to interfaces and provide an implementation for those members. 借助此语言功能,API 作者可以将方法添加到以后版本的接口中,而不会破坏与该接口当前实现的源或二进制文件兼容性。This language feature enables API authors to add methods to an interface in later versions without breaking source or binary compatibility with existing implementations of that interface. 现有的实现继承默认实现 。Existing implementations inherit the default implementation. 此功能使 C# 与面向 Android 或 Swift 的 API 进行互操作,此类 API 支持类似功能。This feature also enables C# to interoperate with APIs that target Android or Swift, which support similar features. 默认接口方法还支持类似于“特征”语言功能的方案。Default interface methods also enable scenarios similar to a "traits" language feature.

默认接口方法会影响很多方案和语言元素。Default interface methods affects many scenarios and language elements. 我们的第一个教程介绍如何使用默认实现更新接口Our first tutorial covers updating an interface with default implementations. 其他教程和参考更新将适时公开发布。Other tutorials and reference updates are coming in time for general release.

在更多位置中使用更多模式More patterns in more places

模式匹配 提供了在相关但不同类型的数据中提供形状相关功能的工具。Pattern matching gives tools to provide shape-dependent functionality across related but different kinds of data. C# 7.0 通过使用 is 表达式和 switch 语句引入了类型模式和常量模式的语法。C# 7.0 introduced syntax for type patterns and constant patterns by using the is expression and the switch statement. 这些功能代表了支持数据和功能分离的编程范例的初步尝试。These features represented the first tentative steps toward supporting programming paradigms where data and functionality live apart. 随着行业转向更多微服务和其他基于云的体系结构,还需要其他语言工具。As the industry moves toward more microservices and other cloud-based architectures, other language tools are needed.

C# 8.0 扩展了此词汇表,这样就可以在代码中的更多位置使用更多模式表达式。C# 8.0 expands this vocabulary so you can use more pattern expressions in more places in your code. 当数据和功能分离时,请考虑使用这些功能。Consider these features when your data and functionality are separate. 当算法依赖于对象运行时类型以外的事实时,请考虑使用模式匹配。Consider pattern matching when your algorithms depend on a fact other than the runtime type of an object. 这些技术提供了另一种表达设计的方式。These techniques provide another way to express designs.

除了可以在新位置使用新模式之外,C# 8.0 还添加了“递归模式” 。In addition to new patterns in new places, C# 8.0 adds recursive patterns. 任何模式表达式的结果都是一个表达式。The result of any pattern expression is an expression. 递归模式只是应用于另一个模式表达式输出的模式表达式。A recursive pattern is simply a pattern expression applied to the output of another pattern expression.

switch 表达式Switch expressions

通常情况下,switch 语句在其每个 case 块中生成一个值。Often, a switch statement produces a value in each of its case blocks. 借助 Switch 表达式 ,可以使用更简洁的表达式语法。Switch expressions enable you to use more concise expression syntax. 只有些许重复的 casebreak 关键字和大括号。There are fewer repetitive case and break keywords, and fewer curly braces. 以下面列出彩虹颜色的枚举为例:As an example, consider the following enum that lists the colors of the rainbow:

public enum Rainbow
{
    Red,
    Orange,
    Yellow,
    Green,
    Blue,
    Indigo,
    Violet
}

如果应用定义了通过 RGB 组件构造而成的 RGBColor 类型,可使用以下包含 switch 表达式的方法,将 Rainbow 转换为 RGB 值:If your application defined an RGBColor type that is constructed from the R, G and B components, you could convert a Rainbow value to its RGB values using the following method containing a switch expression:

public static RGBColor FromRainbow(Rainbow colorBand) =>
    colorBand switch
    {
        Rainbow.Red    => new RGBColor(0xFF, 0x00, 0x00),
        Rainbow.Orange => new RGBColor(0xFF, 0x7F, 0x00),
        Rainbow.Yellow => new RGBColor(0xFF, 0xFF, 0x00),
        Rainbow.Green  => new RGBColor(0x00, 0xFF, 0x00),
        Rainbow.Blue   => new RGBColor(0x00, 0x00, 0xFF),
        Rainbow.Indigo => new RGBColor(0x4B, 0x00, 0x82),
        Rainbow.Violet => new RGBColor(0x94, 0x00, 0xD3),
        _              => throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand)),
    };

这里有几个语法改进:There are several syntax improvements here:

  • 变量位于 switch 关键字之前。The variable comes before the switch keyword. 不同的顺序使得在视觉上可以很轻松地区分 switch 表达式和 switch 语句。The different order makes it visually easy to distinguish the switch expression from the switch statement.
  • case: 元素替换为 =>The case and : elements are replaced with =>. 它更简洁,更直观。It's more concise and intuitive.
  • default 事例替换为 _ 弃元。The default case is replaced with a _ discard.
  • 正文是表达式,不是语句。The bodies are expressions, not statements.

将其与使用经典 switch 语句的等效代码进行对比:Contrast that with the equivalent code using the classic switch statement:

public static RGBColor FromRainbowClassic(Rainbow colorBand)
{
    switch (colorBand)
    {
        case Rainbow.Red:
            return new RGBColor(0xFF, 0x00, 0x00);
        case Rainbow.Orange:
            return new RGBColor(0xFF, 0x7F, 0x00);
        case Rainbow.Yellow:
            return new RGBColor(0xFF, 0xFF, 0x00);
        case Rainbow.Green:
            return new RGBColor(0x00, 0xFF, 0x00);
        case Rainbow.Blue:
            return new RGBColor(0x00, 0x00, 0xFF);
        case Rainbow.Indigo:
            return new RGBColor(0x4B, 0x00, 0x82);
        case Rainbow.Violet:
            return new RGBColor(0x94, 0x00, 0xD3);
        default:
            throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand));
    };
}

属性模式Property patterns

借助属性模式 ,可以匹配所检查的对象的属性。The property pattern enables you to match on properties of the object examined. 请看一个电子商务网站的示例,该网站必须根据买家地址计算销售税。Consider an eCommerce site that must compute sales tax based on the buyer's address. 这种计算不是 Address 类的核心职责。That computation isn't a core responsibility of an Address class. 它会随时间变化,可能比地址格式的更改更频繁。It will change over time, likely more often than address format changes. 销售税的金额取决于地址的 State 属性。The amount of sales tax depends on the State property of the address. 下面的方法使用属性模式从地址和价格计算销售税:The following method uses the property pattern to compute the sales tax from the address and the price:

public static decimal ComputeSalesTax(Address location, decimal salePrice) =>
    location switch
    {
        { State: "WA" } => salePrice * 0.06M,
        { State: "MN" } => salePrice * 0.75M,
        { State: "MI" } => salePrice * 0.05M,
        // other cases removed for brevity...
        _ => 0M
    };

模式匹配为表达此算法创建了简洁的语法。Pattern matching creates a concise syntax for expressing this algorithm.

元组模式Tuple patterns

一些算法依赖于多个输入。Some algorithms depend on multiple inputs. 使用元组模式,可根据表示为元组的多个值进行切换 。Tuple patterns allow you to switch based on multiple values expressed as a tuple. 以下代码显示了游戏“rock, paper, scissors(石头剪刀布)”的切换表达式: :The following code shows a switch expression for the game rock, paper, scissors:

public static string RockPaperScissors(string first, string second)
    => (first, second) switch
    {
        ("rock", "paper") => "rock is covered by paper. Paper wins.",
        ("rock", "scissors") => "rock breaks scissors. Rock wins.",
        ("paper", "rock") => "paper covers rock. Paper wins.",
        ("paper", "scissors") => "paper is cut by scissors. Scissors wins.",
        ("scissors", "rock") => "scissors is broken by rock. Rock wins.",
        ("scissors", "paper") => "scissors cuts paper. Scissors wins.",
        (_, _) => "tie"
    };

消息指示获胜者。The messages indicate the winner. 弃元表示平局(石头剪刀布游戏)的三种组合或其他文本输入。The discard case represents the three combinations for ties, or other text inputs.

位置模式Positional patterns

某些类型包含 Deconstruct 方法,该方法将其属性解构为离散变量。Some types include a Deconstruct method that deconstructs its properties into discrete variables. 如果可以访问 Deconstruct 方法,就可以使用位置模式 检查对象的属性并将这些属性用于模式。When a Deconstruct method is accessible, you can use positional patterns to inspect properties of the object and use those properties for a pattern. 考虑以下 Point 类,其中包含用于为 XY 创建离散变量的 Deconstruct 方法:Consider the following Point class that includes a Deconstruct method to create discrete variables for X and Y:

public class Point
{
    public int X { get; }
    public int Y { get; }

    public Point(int x, int y) => (X, Y) = (x, y);

    public void Deconstruct(out int x, out int y) =>
        (x, y) = (X, Y);
}

此外,请考虑以下表示象限的各种位置的枚举:Additionally, consider the following enum that represents various positions of a quadrant:

public enum Quadrant
{
    Unknown,
    Origin,
    One,
    Two,
    Three,
    Four,
    OnBorder
}

下面的方法使用位置模式 来提取 xy 的值。The following method uses the positional pattern to extract the values of x and y. 然后,它使用 when 子句来确定该点的 QuadrantThen, it uses a when clause to determine the Quadrant of the point:

static Quadrant GetQuadrant(Point point) => point switch
{
    (0, 0) => Quadrant.Origin,
    var (x, y) when x > 0 && y > 0 => Quadrant.One,
    var (x, y) when x < 0 && y > 0 => Quadrant.Two,
    var (x, y) when x < 0 && y < 0 => Quadrant.Three,
    var (x, y) when x > 0 && y < 0 => Quadrant.Four,
    var (_, _) => Quadrant.OnBorder,
    _ => Quadrant.Unknown
};

xy 为 0(但不是两者同时为 0)时,前一个开关中的弃元模式匹配。The discard pattern in the preceding switch matches when either x or y is 0, but not both. Switch 表达式必须要么生成值,要么引发异常。A switch expression must either produce a value or throw an exception. 如果这些情况都不匹配,则 switch 表达式将引发异常。If none of the cases match, the switch expression throws an exception. 如果没有在 switch 表达式中涵盖所有可能的情况,编译器将生成一个警告。The compiler generates a warning for you if you don't cover all possible cases in your switch expression.

可在此模式匹配高级教程中探索模式匹配方法。You can explore pattern matching techniques in this advanced tutorial on pattern matching.

using 声明Using declarations

using 声明 是前面带 using 关键字的变量声明。A using declaration is a variable declaration preceded by the using keyword. 它指示编译器声明的变量应在封闭范围的末尾进行处理。It tells the compiler that the variable being declared should be disposed at the end of the enclosing scope. 以下面编写文本文件的代码为例:For example, consider the following code that writes a text file:

static int WriteLinesToFile(IEnumerable<string> lines)
{
    using var file = new System.IO.StreamWriter("WriteLines2.txt");
    // Notice how we declare skippedLines after the using statement.
    int skippedLines = 0;
    foreach (string line in lines)
    {
        if (!line.Contains("Second"))
        {
            file.WriteLine(line);
        }
        else
        {
            skippedLines++;
        }
    }
    // Notice how skippedLines is in scope here.
    return skippedLines;
    // file is disposed here
}

在前面的示例中,当到达方法的右括号时,将对该文件进行处理。In the preceding example, the file is disposed when the closing brace for the method is reached. 这是声明 file 的范围的末尾。That's the end of the scope in which file is declared. 前面的代码相当于下面使用经典 using 语句的代码:The preceding code is equivalent to the following code that uses the classic using statement:

static int WriteLinesToFile(IEnumerable<string> lines)
{
    // We must declare the variable outside of the using block
    // so that it is in scope to be returned.
    int skippedLines = 0;
    using (var file = new System.IO.StreamWriter("WriteLines2.txt"))
    {
        foreach (string line in lines)
        {
            if (!line.Contains("Second"))
            {
                file.WriteLine(line);
            }
            else
            {
                skippedLines++;
            }
        }
    } // file is disposed here
    return skippedLines;
}

在前面的示例中,当到达与 using 语句关联的右括号时,将对该文件进行处理。In the preceding example, the file is disposed when the closing brace associated with the using statement is reached.

在这两种情况下,编译器将生成对 Dispose() 的调用。In both cases, the compiler generates the call to Dispose(). 如果 using 语句中的表达式不可用,编译器将生成一个错误。The compiler generates an error if the expression in the using statement isn't disposable.

静态本地函数Static local functions

现在可以向本地函数添加 static 修饰符,以确保本地函数不会从封闭范围捕获(引用)任何变量。You can now add the static modifier to local functions to ensure that local function doesn't capture (reference) any variables from the enclosing scope. 这样做会生成 CS8421,“静态本地函数不能包含对 <variable> 的引用”。Doing so generates CS8421, "A static local function can't contain a reference to <variable>."

考虑下列代码。Consider the following code. 本地函数 LocalFunction 访问在封闭范围(方法 M)中声明的变量 yThe local function LocalFunction accesses the variable y, declared in the enclosing scope (the method M). 因此,不能用 static 修饰符来声明 LocalFunctionTherefore, LocalFunction can't be declared with the static modifier:

int M()
{
    int y;
    LocalFunction();
    return y;

    void LocalFunction() => y = 0;
}

下面的代码包含一个静态本地函数。The following code contains a static local function. 它可以是静态的,因为它不访问封闭范围中的任何变量:It can be static because it doesn't access any variables in the enclosing scope:

int M()
{
    int y = 5;
    int x = 7;
    return Add(x, y);

    static int Add(int left, int right) => left + right;
}

可处置的 ref 结构Disposable ref structs

ref 修饰符声明的 struct 可能无法实现任何接口,因此无法实现 IDisposableA struct declared with the ref modifier may not implement any interfaces and so can't implement IDisposable. 因此,要能够处理 ref struct,它必须有一个可访问的 void Dispose() 方法。Therefore, to enable a ref struct to be disposed, it must have an accessible void Dispose() method. 此功能同样适用于 readonly ref struct 声明。This feature also applies to readonly ref struct declarations.

可为空引用类型Nullable reference types

在可为空注释上下文中,引用类型的任何变量都被视为不可为空引用类型 。Inside a nullable annotation context, any variable of a reference type is considered to be a nonnullable reference type. 若要指示一个变量可能为 null,必须在类型名称后面附加 ?,以将该变量声明为可为空引用类型 。If you want to indicate that a variable may be null, you must append the type name with the ? to declare the variable as a nullable reference type.

对于不可为空引用类型,编译器使用流分析来确保在声明时将本地变量初始化为非 Null 值。For nonnullable reference types, the compiler uses flow analysis to ensure that local variables are initialized to a non-null value when declared. 字段必须在构造过程中初始化。Fields must be initialized during construction. 如果没有通过调用任何可用的构造函数或通过初始化表达式来设置变量,编译器将生成警告。The compiler generates a warning if the variable isn't set by a call to any of the available constructors or by an initializer. 此外,不能向不可为空引用类型分配一个可以为 Null 的值。Furthermore, nonnullable reference types can't be assigned a value that could be null.

不对可为空引用类型进行检查以确保它们没有被赋予 Null 值或初始化为 Null。Nullable reference types aren't checked to ensure they aren't assigned or initialized to null. 不过,编译器使用流分析来确保可为空引用类型的任何变量在被访问或分配给不可为空引用类型之前,都会对其 Null 性进行检查。However, the compiler uses flow analysis to ensure that any variable of a nullable reference type is checked against null before it's accessed or assigned to a nonnullable reference type.

可以在可为空引用类型的概述中了解该功能的更多信息。You can learn more about the feature in the overview of nullable reference types. 可以在此可为空引用类型教程中的新应用程序中自行尝试。Try it yourself in a new application in this nullable reference types tutorial. 迁移应用程序以使用可为空引用类型教程中了解迁移现有代码库以使用可为空引用类型的步骤。Learn about the steps to migrate an existing codebase to make use of nullable reference types in the migrating an application to use nullable reference types tutorial.

异步流Asynchronous streams

从 C# 8.0 开始,可以创建并以异步方式使用流。Starting with C# 8.0, you can create and consume streams asynchronously. 返回异步流的方法有三个属性:A method that returns an asynchronous stream has three properties:

  1. 它是用 async 修饰符声明的。It's declared with the async modifier.
  2. 它将返回 IAsyncEnumerable<T>It returns an IAsyncEnumerable<T>.
  3. 该方法包含用于在异步流中返回连续元素的 yield return 语句。The method contains yield return statements to return successive elements in the asynchronous stream.

使用异步流需要在枚举流元素时在 foreach 关键字前面添加 await 关键字。Consuming an asynchronous stream requires you to add the await keyword before the foreach keyword when you enumerate the elements of the stream. 添加 await 关键字需要枚举异步流的方法,以使用 async 修饰符进行声明并返回 async 方法允许的类型。Adding the await keyword requires the method that enumerates the asynchronous stream to be declared with the async modifier and to return a type allowed for an async method. 通常这意味着返回 TaskTask<TResult>Typically that means returning a Task or Task<TResult>. 也可以为 ValueTaskValueTask<TResult>It can also be a ValueTask or ValueTask<TResult>. 方法既可以使用异步流,也可以生成异步流,这意味着它将返回 IAsyncEnumerable<T>A method can both consume and produce an asynchronous stream, which means it would return an IAsyncEnumerable<T>. 下面的代码生成一个从 0 到 19 的序列,在生成每个数字之间等待 100 毫秒:The following code generates a sequence from 0 to 19, waiting 100 ms between generating each number:

public static async System.Collections.Generic.IAsyncEnumerable<int> GenerateSequence()
{
    for (int i = 0; i < 20; i++)
    {
        await Task.Delay(100);
        yield return i;
    }
}

可以使用 await foreach 语句来枚举序列:You would enumerate the sequence using the await foreach statement:

await foreach (var number in GenerateSequence())
{
    Console.WriteLine(number);
}

可以在创建和使用异步流的教程中自行尝试异步流。You can try asynchronous streams yourself in our tutorial on creating and consuming async streams.

索引和范围Indices and ranges

索引和范围为访问序列中的单个元素或范围提供了简洁的语法。Indices and ranges provide a succinct syntax for accessing single elements or ranges in a sequence.

此语言支持依赖于两个新类型和两个新运算符:This language support relies on two new types, and two new operators:

  • System.Index 表示一个序列索引。System.Index represents an index into a sequence.
  • 来自末尾运算符 ^ 的索引,指定一个索引与序列末尾相关。The index from end operator ^, which specifies that an index is relative to the end of the sequence.
  • System.Range 表示序列的子范围。System.Range represents a sub range of a sequence.
  • 范围运算符 ..,用于指定范围的开始和末尾,就像操作数一样。The range operator .., which specifies the start and end of a range as its operands.

让我们从索引规则开始。Let's start with the rules for indexes. 请考虑数组 sequenceConsider an array sequence. 0 索引与 sequence[0] 相同。The 0 index is the same as sequence[0]. ^0 索引与 sequence[sequence.Length] 相同。The ^0 index is the same as sequence[sequence.Length]. 请注意,sequence[^0] 不会引发异常,就像 sequence[sequence.Length] 一样。Note that sequence[^0] does throw an exception, just as sequence[sequence.Length] does. 对于任何数字 n,索引 ^nsequence.Length - n 相同。For any number n, the index ^n is the same as sequence.Length - n.

范围指定范围的开始和末尾 。A range specifies the start and end of a range. 包括此范围的开始,但不包括此范围的末尾,这表示此范围包含开始但不包含末尾 。The start of the range is inclusive, but the end of the range is exclusive, meaning the start is included in the range but the end isn't included in the range. 范围 [0..^0] 表示整个范围,就像 [0..sequence.Length] 表示整个范围。The range [0..^0] represents the entire range, just as [0..sequence.Length] represents the entire range.

请看以下几个示例。Let's look at a few examples. 请考虑以下数组,用其顺数索引和倒数索引进行注释:Consider the following array, annotated with its index from the start and from the end:

var words = new string[]
{
                // index from start    index from end
    "The",      // 0                   ^9
    "quick",    // 1                   ^8
    "brown",    // 2                   ^7
    "fox",      // 3                   ^6
    "jumped",   // 4                   ^5
    "over",     // 5                   ^4
    "the",      // 6                   ^3
    "lazy",     // 7                   ^2
    "dog"       // 8                   ^1
};              // 9 (or words.Length) ^0

可以使用 ^1 索引检索最后一个词:You can retrieve the last word with the ^1 index:

Console.WriteLine($"The last word is {words[^1]}");
// writes "dog"

以下代码创建了一个包含单词“quick”、“brown”和“fox”的子范围。The following code creates a subrange with the words "quick", "brown", and "fox". 它包括 words[1]words[3]It includes words[1] through words[3]. 元素 words[4] 不在该范围内。The element words[4] isn't in the range.

var quickBrownFox = words[1..4];

以下代码使用“lazy”和“dog”创建一个子范围。The following code creates a subrange with "lazy" and "dog". 它包括 words[^2]words[^1]It includes words[^2] and words[^1]. 末尾索引 words[^0] 不包括在内:The end index words[^0] isn't included:

var lazyDog = words[^2..^0];

下面的示例为开始和/或结束创建了开放范围:The following examples create ranges that are open ended for the start, end, or both:

var allWords = words[..]; // contains "The" through "dog".
var firstPhrase = words[..4]; // contains "The" through "fox"
var lastPhrase = words[6..]; // contains "the", "lazy" and "dog"

此外可以将范围声明为变量:You can also declare ranges as variables:

Range phrase = 1..4;

然后可以在 [] 字符中使用该范围:The range can then be used inside the [ and ] characters:

var text = words[phrase];

不仅数组支持索引和范围。Not only arrays support indices and ranges. 也可以将索引和范围用于 stringSpan<T>ReadOnlySpan<T>You also can use indices and ranges with string, Span<T>, or ReadOnlySpan<T>. 有关详细信息,请参阅索引和范围的类型支持For more information, see Type support for indices and ranges.

可在有关索引和范围的教程中详细了解索引和范围。You can explore more about indices and ranges in the tutorial on indices and ranges.

Null 合并赋值Null-coalescing assignment

C# 8.0 引入了 null 合并赋值运算符 ??=C# 8.0 introduces the null-coalescing assignment operator ??=. 仅当左操作数计算为 null 时,才能使用运算符 ??= 将其右操作数的值分配给左操作数。You can use the ??= operator to assign the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null.

List<int> numbers = null;
int? i = null;

numbers ??= new List<int>();
numbers.Add(i ??= 17);
numbers.Add(i ??= 20);

Console.WriteLine(string.Join(" ", numbers));  // output: 17 17
Console.WriteLine(i);  // output: 17

有关详细信息,请参阅 ?? 和 ??= 运算符一文。For more information, see the ?? and ??= operators article.

非托管构造类型Unmanaged constructed types

在 C# 7.3 及更低版本中,构造类型(包含至少一个类型参数的类型)不能为非托管类型In C# 7.3 and earlier, a constructed type (a type that includes at least one type argument) can't be an unmanaged type. 从 C# 8.0 开始,如果构造的值类型仅包含非托管类型的字段,则该类型不受管理。Starting with C# 8.0, a constructed value type is unmanaged if it contains fields of unmanaged types only.

例如,假设泛型 Coords<T> 类型有以下定义:For example, given the following definition of the generic Coords<T> type:

public struct Coords<T>
{
    public T X;
    public T Y;
}

Coords<int> 类型为 C# 8.0 及更高版本中的非托管类型。the Coords<int> type is an unmanaged type in C# 8.0 and later. 与任何非托管类型一样,可以创建指向此类型的变量的指针,或针对此类型的实例在堆栈上分配内存块Like for any unmanaged type, you can create a pointer to a variable of this type or allocate a block of memory on the stack for instances of this type:

Span<Coords<int>> coordinates = stackalloc[]
{
    new Coords<int> { X = 0, Y = 0 },
    new Coords<int> { X = 0, Y = 3 },
    new Coords<int> { X = 4, Y = 0 }
};

有关详细信息,请参阅非托管类型For more information, see Unmanaged types.

嵌套表达式中的 stackallocStackalloc in nested expressions

从 C# 8.0 开始,如果 stackalloc 表达式的结果为 System.Span<T>System.ReadOnlySpan<T> 类型,则可以在其他表达式中使用 stackalloc 表达式:Starting with C# 8.0, if the result of a stackalloc expression is of the System.Span<T> or System.ReadOnlySpan<T> type, you can use the stackalloc expression in other expressions:

Span<int> numbers = stackalloc[] { 1, 2, 3, 4, 5, 6 };
var ind = numbers.IndexOfAny(stackalloc[] { 2, 4, 6 ,8 });
Console.WriteLine(ind);  // output: 1

内插逐字字符串的增强功能Enhancement of interpolated verbatim strings

内插逐字字符串中 $@ 标记的顺序可以任意安排:$@"..."@$"..." 均为有效的内插逐字字符串。Order of the $ and @ tokens in interpolated verbatim strings can be any: both $@"..." and @$"..." are valid interpolated verbatim strings. 在早期 C# 版本中,$ 标记必须出现在 @ 标记之前。In earlier C# versions, the $ token must appear before the @ token.