一探即將到來的 C# 10

前言

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

.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 等的實裝。