前言

本來因為懶不想寫這篇文章,但是不少人表示有興趣,於是最後決定還是寫一下。

.NET 6 最近幾個預覽版一直都在開發體驗(如 hot reload、linker 等)、平臺支援(如 Android、iOS 等)、工具鏈(如 crossgen2、PGO 工具和 wasm 的 AOT 等)、JIT(如 LSRA、Jump threading、PGO 和 guarded devirtualization 以及使 struct 保持在暫存器上等)、GC(如 Regions 等)以及 BCL(如 TimeOnly、DateOnly 以及 Json DOM 等)方面做改進,然而卻一直沒有公佈 C# 10 的任何內容,即使在 Build 2021 大會上也沒有提及這方面內容。然而實際上不少特性的實現已經接近尾聲了,那麼讓我們提前來看看 C# 10 可以為我們帶來什麼東西。

當然,不是所有下面列出的特性都一定會進入 C# 10,也可能會和本文有所出入,我在每一個特性後面加了一個百分比表示最終實裝的可能性,僅供參考。

Backing Fields(60%)

相信不少人在編寫屬性的時候,因為自動屬性不能滿足自己的需求於是不得不改回手動實現屬性,這個時候總是會想“如果能不用手動寫欄位的定義就好了”,現在這個夢想成真了:

private int myInt;
public int MyInt { get => myInt; set => myInt = value; }

C# 10 中新增了一個 field,當使用它時會自動為屬性建立欄位定義,不需要再手動定義欄位了,因此也叫做半自動屬性。

public int MyInt { get => field; set => field = value; }

Record Structs(100%)

Records 此前只支援 class,但是現在同樣支援 struct 啦,於是你可以定義值型別的 record,避免不必要的堆記憶體分配:

record struct Point(int X, int Y);

with on Anonymous Objects(80%)

此前 with 只能配合 records 使用,但是現在它被擴充套件到了匿名物件上,你可以通過 with 來建立匿名物件的副本並且修改它的值啦:

var foo = new { A = 1, B = "test", C = 4.4 };
var bar = foo with { A = 3 };
Console.WriteLine((bar.A, bar.B, bar.C)); // (3, test, 4.4)

Global Usings(80%)

此前 using 語句的生效範圍是單個檔案的,如果你想使用一些 namespace,或者定義一系列的類型別名在整個專案內使用,那麼你就需要這樣:

using System.Linq;
using static System.Math;
using i32 = System.Int32;
using i64 = System.Int64;

然後在每個檔案中重複一遍。但是現在不需要了,你可以定義全域性的 using 了:

global using System.Linq;
global using static System.Math;
global using i32 = System.Int32;
global using i64 = System.Int64;

然後在整個專案中就都可以用了。

File Scoped Namespace(90%)

C# 10 開始你將能夠在檔案頂部指定該檔案的 namespace,而不需要寫一個 namespace 然後把其他程式碼都巢狀在大括號裡面,畢竟絕大多數情況下,我們在寫程式碼時一個檔案裡確實只會寫一個 namespace,這樣可以減少一層巢狀也是很不錯的:

namespace MyProject;

class MyClass
{
// ...
}

如果採用這樣的寫法,每一個檔案將只能宣告一個 namespace。

Constant Interpolated String(100%)

顧名思義,常量字串插值:

const string a = "foo";
const string b = $"{a}_bar"; // foo_bar

常量字串插值將在編譯時完成。

Lambda Improvements(100%)

C# 10 大幅度改進了 lambda,擴充套件了使用場景,並改進了一系列的推導,提出自然委託型別,還函式上升至 first-class。

支援 Attributes

f = [Foo] (x) => x; // 給 lambda 設定
f = [return: Foo] (x) => x; // 給 lambda 返回值設定
f = ([Foo] x) => x; // 給 lambda 引數設定

支援顯示指定返回值型別

此前 C# 的 lambda 返回值型別靠推導,C# 10 開始允許在引數列表最前面顯示指定 lambda 型別了:

f = int () => 4;

支援 ref 等修飾

f = ref int (ref int x) => ref x; // 返回一個引數的引用

First-class Functions

方法可以被隱式轉換到 Delegate,使得函式上升至 first-class。

Delegate f = 1.GetHashCode; // Func<int>
object g = 2.ToString; // object(Func<string>)
var s = (int x) => x; // Func<int, int>

將函式作為變數,然後傳給另一個函式的引數:

void Foo(Func<int> f)
{
Console.WriteLine(f());
} int Bar()
{
return 5;
} var baz = Bar;
Foo(baz);

Natural Delegate Types

lambda 現在會自動建立自然委託型別。

可以用 var 來建立委託了:

var f = () => 1; // Func<int>
var g = string (int x, string y) => $"{y}{x}"; // Func<int, string, string>
var g = "test".GetHashCode; // Func<int>

呼叫 lambdas

得益於上述改進,建立的型別明確的 lambda 可以直接呼叫了。

var zero = ((int x) => x)(0); // 0

Caller Expression Attribute(80%)

現在,CallerArgumentExpression 這個 attribute 終於有用了。藉助這個 attribute,編譯器會自動填充呼叫引數的表示式字串,例如:

void Foo(int value, [CallerArgumentExpression("value")] string? expression = null)
{
Console.WriteLine(expression + " " + value);
}

當你這樣呼叫時:

Foo(4 + 5);

會輸出 4 + 5 = 9。這對測試極其有用,因為你可以輸出 assert 的原表示式了:

static void Assert(bool value, [CallerArgumentExpression("value")] string? expr = null)
{
if (!value) throw new AssertFailureException(expr);
}

default 支援解構(100%)

default 現在支援解構了,因此可以給 tuples 直接賦值。

(int a, int b, int c) = default; // (0, 0, 0)

List Patterns(100%)

Pattern Matching 的最後一塊版圖:list patterns,終於補齊了。

void Foo(List<int> list)
{
switch (list)
{
case [4]:
Console.WriteLine("長度為 4");
break;
case { 1, 2, 3 }:
Console.WriteLine("元素是 1, 2, 3");
break;
case { 1, 2, ..var x, 5 }:
Console.WriteLine($"前兩個元素是 1, 2,最後一個元素是 5,倒數第二個元素是 {x}");
break;
default:
Console.WriteLine("其他");
}
}

同樣的,該 pattern 也是 recursive 的,因此你可以巢狀其他 patterns。

除了上述 switch statements 的用法,在 if 以及 switch expressions 等地方也同樣可用,例如:

void Foo(List<int> list)
{
var result = list switch
{
[4] => ...,
{ 1, 2, 3 } => ...,
{ 1, 2, ..var x, 5 } => ...,
_ => ...
};
}

Abstract Static Member in Interfaces(100%)

C# 10 中,介面可以宣告抽象靜態成員了,.NET 的型別系統正式具備 virtual static dispatch 能力。

例如,你想定義一個可加而且有零的介面 IMonoid

interface IMonoid<T> where T : IMonoid<T>
{
abstract static T Zero { get; }
abstract static T operator+(T l, T r);
}

然後可以對其進行實現,例如這裡的 MyInt

public class MyInt : IMonoid<MyInt>
{
public MyInt(int val) { Value = val; } public static MyInt Zero { get; } = new MyInt(0);
public static MyInt operator+(MyInt l, MyInt r) => new MyInt(l.Value + r.Value); public int Value { get; }
}

然後就能寫出一個方法對 IMoniod<T> 進行求和了,這裡為了方便寫成擴充套件方法:

public static class IMonoidExtensions
{
public static T Sum<T>(this IEnumerable<T> t) where T : IMonoid<T>
{
var result = T.Zero;
foreach (var i in t) result += i;
return result;
}
}

最後呼叫:

List<MyInt> list = new() { new(1), new(2), new(3) };
Console.WriteLine(list.Sum().Value); // 6

這個特性同樣也會對 .NET BCL 做出改進,會新增諸如 IAddable<T>INumeric<T> 的介面,併為適用的已有型別實現。

總結

以上就是在 C# 10 的大部分新特性介紹了,雖然不保證最終效果和本文效果一致,但是也能看到一個大概的方向。

從 interface 的改進上我們可以看到一個好的預兆:.NET 終於開始動型別系統了。2008 年至今幾乎沒有變過的 CTS 顯然逐漸不能適應語言發展的需要,而 .NET 團隊也明確給出了資訊表明要在 C# 11 前後對型別系統集中進行改進,現在只是一個開始,相信不久之後也將能看到 traits、union types、bottom types 和 HKT 等的實裝。