自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

關(guān)于 C# 12 新增功能實操!

開發(fā)
今天咱們一起來探索并實踐 C# 12 引入的全新功能!使用這些功能需要使用最新的 Visual Studio 2022 版本或安裝 .NET 8 SDK 。

今天咱們一起來探索并實踐 C# 12 引入的全新功能!

注意:使用這些功能需要使用最新的 Visual Studio 2022 版本或安裝 .NET 8 SDK 。

主構(gòu)造函數(shù)

主構(gòu)造函數(shù)允許你直接在類定義中聲明構(gòu)造函數(shù)參數(shù),并自動生成相應(yīng)的屬性。

主構(gòu)造函數(shù)參數(shù)的最常見用途包括:

  • 作為 base() 構(gòu)造函數(shù)調(diào)用的參數(shù)。
  • 初始化成員字段或?qū)傩浴?/li>
  • 引用實例成員中的構(gòu)造函數(shù)參數(shù)。

代碼示例

將任何參數(shù)放在類型名稱后面的括號中:

    public class CSharp12GrammarExercise
    {
        public static void OutputPrint()
        {
            var person = new Person("追逐時光者", 30);
            Console.WriteLine($"{person.Name}, {person.Age}");
        }
    }

    public class Person(string name, int age)
    {
        public string Name => name;
        public int Age => age;
    }

以下代碼初始化從主構(gòu)造函數(shù)參數(shù)計算的兩個只讀屬性:

    public class CSharp12GrammarExercise
    {
        public static void OutputPrint()
        {
            // 創(chuàng)建 Distance 結(jié)構(gòu)體實例
            Distance distance = new Distance(10, 55);
            // 訪問 Magnitude 和 Direction 屬性
            Console.WriteLine($"Magnitude: {distance.Magnitude},Direction: {distance.Direction}");
        }
    }

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

集合表達(dá)式

集合表達(dá)式引入了一種新的簡潔語法,用于創(chuàng)建常用集合值??梢允褂谜归_運算符(..)將其他集合內(nèi)聯(lián)到這些值中。

(1) 下面的示例展示了集合表達(dá)式的用法:

        public static void CollectionExpressions()
        {
            // 創(chuàng)建一個數(shù)組
            int[] array = [55, 99, 100, 33];

            // 創(chuàng)建一個列表
            List<string> list = ["one", "two", "three", "five", "追逐時光者"];

            // 創(chuàng)建一個 Span
            Span<char> span = ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'k'];

            // 創(chuàng)建一個交錯二維數(shù)組
            int[][] two2D = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [88, 8, 9]];
        }

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

展開運算符(集合表達(dá)式中的 ..)可將其參數(shù)替換為該集合中的元素。 參數(shù)必須是集合類型。 以下示例演示了展開運算符的工作原理:

            int[] item0 = [88, 2, 3];
            int[] item1 = [22, 5, 6];
            int[] item2 = [7, 99, 9];
            int[] totalList = [.. item0, .. item1, .. item2];
            foreach (var element in totalList)
            {
                Console.Write($"{element}, ");
            }

沒有.. 會有異常:

正常輸出:

內(nèi)聯(lián)數(shù)組

內(nèi)聯(lián)數(shù)組用于提高應(yīng)用程序性能,允許在結(jié)構(gòu)體中創(chuàng)建固定大小的數(shù)組。雖然你可能不會自己聲明內(nèi)聯(lián)數(shù)組,但當(dāng)它們通過 System.Span<T> 或 System.ReadOnlySpan<T> 從運行時 API 暴露出來時,你可以透明地使用它們。內(nèi)聯(lián)數(shù)組提供與不安全固定大小緩沖區(qū)類似的性能特性。

內(nèi)聯(lián)數(shù)組的聲明與下面的結(jié)構(gòu)類似:

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

你可以像使用其他數(shù)組一樣使用它們:

        public static void InlineArrays()
        {
            var buffer = new Buffer();
            for (int i = 0; i < 20; i++)
            {
                buffer[i] = i;
            }

            foreach (var i in buffer)
            {
                Console.WriteLine(i);
            }
        }

默認(rèn) lambda 參數(shù)

現(xiàn)在可以為 Lambda 表達(dá)式的參數(shù)定義默認(rèn)值,語法和規(guī)則與將參數(shù)的默認(rèn)值添加到任何方法或本地函數(shù)相同。

如果 lambda 表達(dá)式只有一個輸入?yún)?shù),則括號是可選的:

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

兩個或更多輸入?yún)?shù)使用逗號加以分隔:

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

可以顯式指定類型,如下面的示例所示:

注意:輸入?yún)?shù)類型必須全部為顯式或全部為隱式;否則,便會生成 CS0748 編譯器錯誤!!

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

任何類型的別名

可以使用 using 別名指令創(chuàng)建任何類型的別名,而不僅僅是命名類型。也就是說,你可以為元組類型、數(shù)組類型、指針類型或其他不安全類型創(chuàng)建語義別名。

使用 using 關(guān)鍵字為元組類型創(chuàng)建別名,并進(jìn)行調(diào)用:

using PointTest = (int item1, int item2);

namespace HelloDotNetGuide.CSharp語法
{
    public class CSharp12GrammarExercise
    {
        public static void OutputPrint()
        {
            //使用 using 關(guān)鍵字為元組類型創(chuàng)建別名,并進(jìn)行調(diào)用:
            PointTest point = (10, 20);
            Console.WriteLine($"輸出:Item1={point.Item1}, Item2={point.Item2}");
        }
    }
}

參考文章

  • 詳細(xì)功能介紹請閱讀微軟官方文檔:https://learn.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-12
  • 文章示例源碼地址:https://github.com/YSGStudyHards/DotNetGuide/blob/main/DotNetGuidePractice/HelloDotNetGuide/CSharp%E8%AF%AD%E6%B3%95/CSharp12GrammarExercise.cs
責(zé)任編輯:趙寧寧 來源: 追逐時光者
相關(guān)推薦

2010-04-16 11:22:08

Oracle存儲過程

2024-04-23 08:08:04

C#

2024-11-20 16:02:47

.NET 9LINQ開發(fā)

2010-05-14 17:56:16

SQL優(yōu)化索引

2025-03-10 05:00:00

JIT技術(shù)語言

2009-08-18 09:26:07

C#線程功能

2010-01-06 10:38:16

Linux安裝JDK

2010-04-12 09:36:29

Oacle merge

2009-09-07 15:40:06

2009-09-02 17:29:10

C# TextBox換

2009-08-27 18:05:54

C#索引功能

2009-08-18 09:33:49

C#特有線程功能

2010-04-09 10:13:13

Oracle數(shù)據(jù)字典

2010-04-15 14:18:30

Oracle創(chuàng)建

2010-05-10 17:00:53

Oracle死鎖進(jìn)程

2010-04-13 14:00:00

Oracle inse

2024-04-28 10:52:25

CentOS系統(tǒng)RHEL系統(tǒng)

2021-09-22 15:36:31

勒索軟件攻擊數(shù)據(jù)泄露

2009-12-01 18:03:56

Linux版本

2010-05-18 12:24:16

MySQL binlo
點贊
收藏

51CTO技術(shù)棧公眾號