轉眼之間C#都已經更新到了12了,那么C# 12 中新增的八大功能大家都了解過嗎?今天我們來簡單講解一下C# 12 中的八大新增功能。
在 Visual Studio 2022 版本 17.6 預覽版 2 中引入。
從 C# 12 開始,可以在類和結構中聲明主構造函數。主構造函數參數都在類的整個主體的范圍內。 為了確保顯式分配所有主構造函數參數,所有顯式聲明的構造函數都必須使用 this() 語法調用主構造函數。 將主構造函數添加到 class 可防止編譯器聲明隱式無參數構造函數。 在 struct 中,隱式無參數構造函數初始化所有字段,包括 0 位模式的主構造函數參數。
將任何參數放在類型名稱后面的括號中:
public class NameParameter(string name){ public string Name => name;}
以下代碼初始化從主構造函數參數計算的兩個只讀屬性:
public readonly struct Distance(double dx, double dy){ public readonly double Magnitude { get; } = Math.Sqrt(dx * dx + dy * dy); public readonly double Direction { get; } = Math.Atan2(dy, dx);}
在 Visual Studio 2022 版本 17.7 預覽版 5 中引入。
集合表達式引入了一種新的簡潔語法,用于創建常用集合值。可以使用展開運算符(..)將其他集合內聯到這些值中。
// Create an arrayint[] array = [55, 99, 100, 33];// Create a listList<string> list = ["one", "two", "three", "five", "追逐時光者"];// Create a spanSpan<char> c = ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'k'];// Create a jagged 2D arrayint[][] two2D = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [88, 8, 9]];
展開運算符(集合表達式中的 ..)可將其參數替換為該集合中的元素。 參數必須是集合類型。 以下示例演示了展開運算符的工作原理:
int[] item0 = [88, 2, 3];int[] item1 = [22, 5, 6];int[] item2 = [7, 99, 9];int[] single = [.. item0, .. item1, .. item2];foreach (var element in single){ Console.Write($"{element}, ");}
沒有.. 會有異常:
正常輸出:
在 Visual Studio 2022 版本 17.7 預覽版 3 中引入。
運行時團隊和其他庫作者使用內聯數組來提高應用程序的性能。通過內聯數組,開發人員可以在結構類型中創建固定大小的數組。具有內聯緩沖區的結構體應具有與不安全固定大小緩沖區類似的性能特性。
內聯數組的聲明與下面的結構類似:
[System.Runtime.CompilerServices.InlineArray(20)] public struct Buffer { private int _element0; }
你可以像使用其他數組一樣使用它們:
public static void Test() { var buffer = new Buffer(); for (int i = 0; i < 20; i++) { buffer[i] = i; } foreach (var i in buffer) { Console.WriteLine(i); } }
在 Visual Studio 2022 版本 17.5 預覽版 2 中引入。
可以為 Lambda 表達式的參數定義默認值。 語法和規則與將參數的默認值添加到任何方法或本地函數相同。
如果 lambda 表達式只有一個輸入參數,則括號是可選的:
Func<double, double> cube = x => x * x * x;
兩個或更多輸入參數使用逗號加以分隔:
Func<int, int, bool> testForEquality = (x, y) => x == y;
可以顯式指定類型,如下面的示例所示:
Func<int, string, bool> isTooLong = (int x, string s) => s.Length > x;
注意:輸入參數類型必須全部為顯式或全部為隱式;否則,便會生成 CS0748 編譯器錯誤!!
在 Visual Studio 2022 版本 17.8 預覽版 2 中引入。
ref readonly修飾符表示方法希望參數是一個變量,而不是一個非變量的表達式。不是變量的表達式包括常量、方法返回值和屬性。如果參數不是變量,編譯器會發出警告。
在 Visual Studio 2022 版本 17.6 預覽版 3 中引入。
可以使用 using alias 指令來別名任何類型,而不僅僅是命名類型。也就是說,你可以為元組類型、數組類型、指針類型或其他不安全類型創建語義別名。
使用 using 關鍵字為元組類型創建別名,并進行調用:
using PointTest = (int x, int y);namespace Csharp12{ internal class Class1 { public static void Test() { PointTest point = (10, 20); Console.WriteLine($"Point coordinates: X={point.Item1}, Y={point.Item2}"); } }}
在 Visual Studio 2022 版本 17.7 預覽版 3 中引入。
可以使用 System.Diagnostics.CodeAnalysis.ExperimentalAttribute 來標記類型、方法或程序集,以指示實驗性特征。 如果訪問使用 ExperimentalAttribute 注釋的方法或類型,編譯器將發出警告。 用 Experimental 特性標記的程序集中包含的所有類型都是實驗性的。
示例代碼:
namespace Csharp12{ [AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Module | System.AttributeTargets.Property | System.AttributeTargets.Struct)] public class ExperimentalAttribute : Attribute { public ExperimentalAttribute() { } } [Experimental] public class ExperimentalClass { public void DoSomething() { Console.WriteLine("Doing something experimental..."); } } internal class Class1 { public static void Test() { ExperimentalClass exp = new ExperimentalClass(); exp.DoSomething(); } }}
預覽功能在 Visual Studio 2022 版本 17.7 預覽版 3 中引入。
攔截器是一種方法,該方法可以在編譯時以聲明方式將對可攔截方法的調用替換為對其自身的調用。 通過讓攔截器聲明所攔截調用的源位置,可以進行這種替換。 攔截器可以向編譯中(例如在源生成器中)添加新代碼,從而提供更改現有代碼語義的有限能力。
注意:攔截器是一項試驗性功能,在 C# 12 的預覽模式下提供。 在將來的版本中,該功能可能會發生中斷性變更或被刪除。 因此,不建議將其用于生產或已發布的應用程序。
要使用攔截器,用戶項目必須指定 <InterceptorsPreviewNamespaces> 屬性。這是允許包含攔截器的命名空間列表。
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.AspNetCore.Http.Generated;MyLibrary.Generated</InterceptorsPreviewNamespaces>
本文鏈接:http://www.tebozhan.com/showinfo-26-84718-0.htmlC# 12 中新增的幾大功能你都知道嗎?
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: Meta如何將其緩存一致性提高至99.99999999
下一篇: 如何編寫可讀性高的 C/C++代碼?