亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

如何在C#9 中使用頂級(jí)程序 (top-level)

 更新時(shí)間:2021年03月31日 11:50:39   作者:碼農(nóng)讀書(shū)  
這篇文章主要介紹了如何在C#9 中使用頂級(jí)程序 (top-level),幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

當(dāng)我們用 C# 進(jìn)行編碼的時(shí)候,總需要寫(xiě)很多的模板代碼,即使是最簡(jiǎn)單的 console 程序,想象一下,如果去測(cè)試一個(gè) 類(lèi)庫(kù) 或者 API 的功能,通常你會(huì)用 Console 程序去實(shí)現(xiàn),在開(kāi)始工作的時(shí)候會(huì)發(fā)現(xiàn)你受到了 C# 標(biāo)準(zhǔn)模板的限制,業(yè)務(wù)邏輯必須要寫(xiě)在 Main 里,如下代碼所示:

    class Program
    {
        static void Main(string[] args)
        {
            //todo
        }
    }

頂級(jí)程序 是 C#9 中引入的一個(gè)新概念,允許你直接寫(xiě)自己的業(yè)務(wù)邏輯而不必受到模板代碼的限制,頂級(jí)程序 是一個(gè)非常🐂👃的特性,可以讓代碼更加的干凈,簡(jiǎn)短和可讀,你可以通過(guò)頂級(jí)程序去探索新的 idea,這篇文章將會(huì)討論如何在 C#9 中使用頂級(jí)程序。

頂級(jí)程序

在 C# 9.0 之前,下面的寫(xiě)法在 Console 程序中已經(jīng)是最小化的了。

using System;
namespace IDG_Top_Level_Programs_Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

在 C# 9.0 時(shí)代,可以祭出 頂級(jí)程序 來(lái)消除那些煩人的模板代碼,讓代碼的邏輯意圖更明顯,改造后的代碼如下:

using System;
Console.WriteLine("Hello World!");

頂級(jí)程序中的方法

你也可以在頂級(jí)程序中使用方法,如下例子所示:

System.Console.WriteLine(DisplayMessage("Joydip!"));
System.Console.Read();
static string DisplayMessage(string name)
{
    return "Hello, " + name;
}

程序跑起來(lái)后,控制臺(tái)將會(huì)輸出:Hello, Joydip!

頂級(jí)程序中的類(lèi)

你也可以在頂級(jí)程序中使用類(lèi),結(jié)構(gòu)體,枚舉,下面的代碼展示了如何使用。

System.Console.WriteLine(new Author().DisplayMessage("Joydip!"));
System.Console.Read();
public class Author
{
    public string DisplayMessage(string name)
    {
        return "Hello, " + name;
    }
}

頂級(jí)程序的原理分析

現(xiàn)在我們來(lái)分析一下,頂級(jí)程序的底層邏輯到底是怎么樣的,它本質(zhì)上是一種語(yǔ)法糖,一種編譯器的特性,也就是說(shuō)你沒(méi)有寫(xiě)模板代碼的時(shí)候,編譯器會(huì)幫你生成,替你負(fù)重前行,參考下面的代碼段。

using System;
Console.WriteLine("Hello World!");

然后用在線工具 SharpLab https://sharplab.io/  看一下編譯器替你補(bǔ)齊的代碼。

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[CompilerGenerated]
internal static class <Program>$
{
    private static void <Main>$(string[] args)
    {
        Console.WriteLine("Hello World!");
    }
}

總的來(lái)說(shuō),頂級(jí)程序 非常適合那些想 快速試錯(cuò),驗(yàn)證想法 的場(chǎng)景,有一點(diǎn)要特別注意,應(yīng)用程序中只能僅有一個(gè)文件使用 頂級(jí)程序,如果存在多個(gè),編譯器會(huì)拋出錯(cuò)誤的,還有一點(diǎn),如果你是 C# 新手,你可能不理解頂級(jí)程序的底層邏輯,更好的方式就是老老實(shí)實(shí)的使用原生模板代碼,當(dāng)你主宰了 Main 后,你將會(huì)理解 頂級(jí)程序 是多么的短小精悍!

以上就是如何在C#9 中使用頂級(jí)程序 (top-level)的詳細(xì)內(nèi)容,更多關(guān)于C#9 中使用頂級(jí)程序 (top-level)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論