AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

C# 12 中新增的幾大功能你都知道嗎?

來源: 責編: 時間:2024-04-23 09:13:58 150觀看
導讀轉眼之間C#都已經更新到了12了,那么C# 12 中新增的八大功能大家都了解過嗎?今天我們來簡單講解一下C# 12 中的八大新增功能。一、主構造函數在 Visual Studio 2022 版本 17.6 預覽版 2 中引入。從 C# 12 開始,可以在類和

轉眼之間C#都已經更新到了12了,那么C# 12 中新增的八大功能大家都了解過嗎?今天我們來簡單講解一下C# 12 中的八大新增功能。uUq28資訊網——每日最新資訊28at.com

uUq28資訊網——每日最新資訊28at.com

一、主構造函數

在 Visual Studio 2022 版本 17.6 預覽版 2 中引入。uUq28資訊網——每日最新資訊28at.com

uUq28資訊網——每日最新資訊28at.com

從 C# 12 開始,可以在類和結構中聲明主構造函數。主構造函數參數都在類的整個主體的范圍內。 為了確保顯式分配所有主構造函數參數,所有顯式聲明的構造函數都必須使用 this() 語法調用主構造函數。 將主構造函數添加到 class 可防止編譯器聲明隱式無參數構造函數。 在 struct 中,隱式無參數構造函數初始化所有字段,包括 0 位模式的主構造函數參數。uUq28資訊網——每日最新資訊28at.com

1.主構造函數參數的最常見用途包括:

  • 作為 base() 構造函數調用的參數。
  • 初始化成員字段或屬性。
  • 引用實例成員中的構造函數參數。

2.代碼示例

將任何參數放在類型名稱后面的括號中:uUq28資訊網——每日最新資訊28at.com

public class NameParameter(string name){    public string Name => name;}

以下代碼初始化從主構造函數參數計算的兩個只讀屬性:uUq28資訊網——每日最新資訊28at.com

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 中引入。uUq28資訊網——每日最新資訊28at.com

uUq28資訊網——每日最新資訊28at.com

集合表達式引入了一種新的簡潔語法,用于創建常用集合值。可以使用展開運算符(..)將其他集合內聯到這些值中。uUq28資訊網——每日最新資訊28at.com

1.下面的示例展示了集合表達式的用法:

// 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]];

2.展開運算符(..)示例代碼:

展開運算符(集合表達式中的 ..)可將其參數替換為該集合中的元素。 參數必須是集合類型。 以下示例演示了展開運算符的工作原理:uUq28資訊網——每日最新資訊28at.com

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}, ");}

沒有.. 會有異常:uUq28資訊網——每日最新資訊28at.com

uUq28資訊網——每日最新資訊28at.com

正常輸出:uUq28資訊網——每日最新資訊28at.com

uUq28資訊網——每日最新資訊28at.com

三、內聯數組

在 Visual Studio 2022 版本 17.7 預覽版 3 中引入。uUq28資訊網——每日最新資訊28at.com

uUq28資訊網——每日最新資訊28at.com

運行時團隊和其他庫作者使用內聯數組來提高應用程序的性能。通過內聯數組,開發人員可以在結構類型中創建固定大小的數組。具有內聯緩沖區的結構體應具有與不安全固定大小緩沖區類似的性能特性。uUq28資訊網——每日最新資訊28at.com

內聯數組的聲明與下面的結構類似:uUq28資訊網——每日最新資訊28at.com

    [System.Runtime.CompilerServices.InlineArray(20)]    public struct Buffer    {        private int _element0;    }

你可以像使用其他數組一樣使用它們:uUq28資訊網——每日最新資訊28at.com

        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);            }        }

四、Lambda 表達式中的可選參數

在 Visual Studio 2022 版本 17.5 預覽版 2 中引入。uUq28資訊網——每日最新資訊28at.com

uUq28資訊網——每日最新資訊28at.com

可以為 Lambda 表達式的參數定義默認值。 語法和規則與將參數的默認值添加到任何方法或本地函數相同。uUq28資訊網——每日最新資訊28at.com

如果 lambda 表達式只有一個輸入參數,則括號是可選的:uUq28資訊網——每日最新資訊28at.com

Func<double, double> cube = x => x * x * x;

兩個或更多輸入參數使用逗號加以分隔:uUq28資訊網——每日最新資訊28at.com

Func<int, int, bool> testForEquality = (x, y) => x == y;

可以顯式指定類型,如下面的示例所示:uUq28資訊網——每日最新資訊28at.com

Func<int, string, bool> isTooLong = (int x, string s) => s.Length > x;

注意:輸入參數類型必須全部為顯式或全部為隱式;否則,便會生成 CS0748 編譯器錯誤!!uUq28資訊網——每日最新資訊28at.com

五、ref readonly參數

在 Visual Studio 2022 版本 17.8 預覽版 2 中引入。uUq28資訊網——每日最新資訊28at.com

uUq28資訊網——每日最新資訊28at.com

ref readonly修飾符表示方法希望參數是一個變量,而不是一個非變量的表達式。不是變量的表達式包括常量、方法返回值和屬性。如果參數不是變量,編譯器會發出警告。uUq28資訊網——每日最新資訊28at.com

六、任何類型的別名

在 Visual Studio 2022 版本 17.6 預覽版 3 中引入。uUq28資訊網——每日最新資訊28at.com

uUq28資訊網——每日最新資訊28at.com

可以使用 using alias 指令來別名任何類型,而不僅僅是命名類型。也就是說,你可以為元組類型、數組類型、指針類型或其他不安全類型創建語義別名。uUq28資訊網——每日最新資訊28at.com

使用 using 關鍵字為元組類型創建別名,并進行調用:uUq28資訊網——每日最新資訊28at.com

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}");        }    }}

七、Experimental屬性

在 Visual Studio 2022 版本 17.7 預覽版 3 中引入。uUq28資訊網——每日最新資訊28at.com

uUq28資訊網——每日最新資訊28at.com

可以使用 System.Diagnostics.CodeAnalysis.ExperimentalAttribute 來標記類型、方法或程序集,以指示實驗性特征。 如果訪問使用 ExperimentalAttribute 注釋的方法或類型,編譯器將發出警告。 用 Experimental 特性標記的程序集中包含的所有類型都是實驗性的。uUq28資訊網——每日最新資訊28at.com

示例代碼:uUq28資訊網——每日最新資訊28at.com

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 中引入。uUq28資訊網——每日最新資訊28at.com

uUq28資訊網——每日最新資訊28at.com

攔截器是一種方法,該方法可以在編譯時以聲明方式將對可攔截方法的調用替換為對其自身的調用。 通過讓攔截器聲明所攔截調用的源位置,可以進行這種替換。 攔截器可以向編譯中(例如在源生成器中)添加新代碼,從而提供更改現有代碼語義的有限能力。uUq28資訊網——每日最新資訊28at.com

注意:攔截器是一項試驗性功能,在 C# 12 的預覽模式下提供。 在將來的版本中,該功能可能會發生中斷性變更或被刪除。 因此,不建議將其用于生產或已發布的應用程序。uUq28資訊網——每日最新資訊28at.com

uUq28資訊網——每日最新資訊28at.com

要使用攔截器,用戶項目必須指定 <InterceptorsPreviewNamespaces> 屬性。這是允許包含攔截器的命名空間列表。uUq28資訊網——每日最新資訊28at.com

<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++代碼?

標簽:
  • 熱門焦點
Top