日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > C# >内容正文

C#

VS 2019 要来了,是时候了解一下 C# 8.0 新功能

發(fā)布時(shí)間:2023/12/4 C# 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 VS 2019 要来了,是时候了解一下 C# 8.0 新功能 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

近日,微軟發(fā)布了 Visual Studio 2019 的發(fā)布日期,2019 年 4 月 2 日 Visual Studio 2019?將正式和大家見(jiàn)面,同時(shí)微軟還將提供發(fā)布現(xiàn)場(chǎng)實(shí)時(shí)直播。

除了 Visual Studio 2019 自身之外,VS 2019 的發(fā)布還牽動(dòng)著很多 C# 開(kāi)發(fā)者的心。雖然一個(gè)月之前發(fā)布的 Visual Studio 2019 Preview 版本已經(jīng)可以試用 C# 的某些新功能,但還有一些是不可試用的。

下面我們就來(lái)看一下微軟官方對(duì) C#8.0 重要功能的概述。

可空的引用類型

此功能的目的是防止無(wú)處不在的空引用異常,空引用異常已經(jīng)困擾面向?qū)ο缶幊贪雮€(gè)世紀(jì)了。該功能將阻止開(kāi)放者將 null 值放入到普通的引用類型中,例如 String 類型不可為空。但它不是強(qiáng)制性的 error,而是比較溫和的 warning。

這些異常現(xiàn)在已經(jīng)過(guò)了半個(gè)世紀(jì)的面向?qū)ο缶幊獭?br />它阻止你 null 進(jìn)入普通的引用類型,例如 string- 它使這些類型不可為空!它是溫和的,有警告,而不是錯(cuò)誤。但是在現(xiàn)有代碼上會(huì)出現(xiàn)新警告,因此您必須選擇使用該功能(您可以在項(xiàng)目,文件甚至源代碼級(jí)別執(zhí)行此功能)。
string s = null; // Warning: Assignment of null to non-nullable reference type
如果你想要使用 null 怎么辦?可以使用可為空的引用類型,例如 string?:
string? s = null; // Ok
當(dāng)你使用了可空引用時(shí),需要先檢查一下其是否為 null,編譯器會(huì)分析代碼流,以查看 null 值是否可以將其用于您使用它的位置:

void M(string? s)

{

Console.WriteLine(s.Length); // Warning: Possible null reference exception

if (s != null)

{

Console.WriteLine(s.Length); // Ok: You won't get here if s is null

}

}

C# 允許表達(dá)可空的意圖,但是在不遵守規(guī)則時(shí)會(huì)發(fā)出警告。

異步流

C#5.0 的 async / await 功能允許在簡(jiǎn)單的代碼中使用(并生成)異步結(jié)果,而無(wú)需回調(diào):

async Task<int> GetBigResultAsync()

{

var result = await GetResultAsync();

if (result > 20) return result;

else return -1;

}

下面我們來(lái)介紹一下大家期待已久的 IAsyncEnumerable,?異步版本的 IEnumerable。該語(yǔ)言允許 await foreach 使用元素,并使用 yield return 生成元素。

async IAsyncEnumerable<int> GetBigResultsAsync()

{

await foreach (var result in GetResultsAsync())

{

if (result > 20) yield return result;

}

}

范圍和索引

我們正在添加一個(gè)可用于索引的 Index 類型。你可以使用 int 從頭創(chuàng)建,也可以使用 ^ 從末尾開(kāi)始計(jì)算前綴運(yùn)算符:
Index i1 = 3; // number 3 from beginning
Index i2 = ^4; // number 4 from end
int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine($"{a[i1]}, {a[i2]}"); // “3, 6”
另外,我們還引入了一個(gè) Range 類型,它由兩個(gè) Indexes?組成,一個(gè)用于開(kāi)始,一個(gè)用于結(jié)束,并且可以用 x…y?范圍表達(dá)式編寫(xiě)。
可以使用 a 進(jìn)行索引 Range 以生成切片:
var slice = a[i1…i2]; // { 3, 4, 5 }

接口成員的默認(rèn)實(shí)現(xiàn)

今天,大家對(duì)于界面都有這樣一個(gè)需求:在不破壞現(xiàn)有狀態(tài)的情況下添加一個(gè)成員。

在 C#8.0 中,我們會(huì)為接口成員提供一個(gè)主體。如果有人沒(méi)有實(shí)現(xiàn)該成員(或者是在編寫(xiě)代碼時(shí)還沒(méi)有實(shí)現(xiàn)),會(huì)獲得默認(rèn)實(shí)現(xiàn)。

interface ILogger

{

void Log(LogLevel level, string message);

void Log(Exception ex) => Log(LogLevel.Error, ex.ToString()); // New overload

}


class ConsoleLogger : ILogger

{

public void Log(LogLevel level, string message) { ... }

// Log(Exception) gets default implementation

}


在 ConsoleLogger 類不需要實(shí)現(xiàn) ILogger 的 Log(Exception) 重載,因?yàn)樗呀?jīng)默認(rèn)實(shí)現(xiàn)了。現(xiàn)在只要給當(dāng)前實(shí)現(xiàn)者提供了默認(rèn)實(shí)現(xiàn),就可以向現(xiàn)有公共接口添加新成員。

遞歸模式

我們?cè)试S pattern 中包含其他 pattern:

IEnumerable<string> GetEnrollees()

{

foreach (var p in People)

{

if (p is Student { Graduated: false, Name: string name }) yield return name;

}

}

pattern Student { Graduated: false, Name: string name }主要檢查 Person 是 a?Student,然后將常量 pattern false 應(yīng)用于其 Graduated 屬性以查看它們是否仍然已注冊(cè),并將 pattern string name 應(yīng)用于其 Name 屬性以獲取其名稱(如果為非 null)。因此,如果 p 是一個(gè) Student,尚未畢業(yè)并且姓名非空,那么我們就可以 yield return 這個(gè)名字。

Switch 表達(dá)式

帶有 pattern 的 switch 語(yǔ)句在 C#7.0 中已經(jīng)非常強(qiáng)大了,但是編寫(xiě)起來(lái)卻很麻煩,而 Switch 表達(dá)式卻是一個(gè)解決這種問(wèn)題的、“輕量級(jí)”的版本。

var area = figure switch

{

Line _ ? ? ?=> 0,

Rectangle r => r.Width * r.Height,

Circle c ? ?=> Math.PI * c.Radius * c.Radius,

_ ? ? ? ? ? => throw new UnknownFigureException(figure)

};

目標(biāo)類型的新表達(dá)式

在許多情況下,往往創(chuàng)建新對(duì)象時(shí),類型已經(jīng)從上下文中給出。在這些情況下,我們會(huì)讓你省略類型:
Point[] ps = { new (1, 4), new (3,-2), new (9, 5) }; // all Points

C# 大版本關(guān)鍵更新回顧

C#1.0(Visual Studio .NET)

  • Classes

  • Structs

  • Interfaces

  • Events

  • Properties

  • Delegates

  • Expressions

  • Statements

  • Attributes

  • Literal

C#2(VS 2005)

  • Generics

  • Partial types

  • Anonymous methods

  • Iterators

  • Nullable types

  • Getter/setter separate accessibility

  • Method group conversions (delegates)

  • Static classes

  • Delegate inferenc

C#3(VS 2008)

  • Implicitly typed local variables

  • Object and collection initializers

  • Auto-Implemented properties

  • Anonymous types

  • Extension methods

  • Query expressions

  • Lambda expression

  • Expression trees

  • Partial methods

C#4(VS 2010)

  • Dynamic binding

  • Named and optional arguments

  • Co- and Contra-variance for generic delegates and interfaces

  • Embedded interop types (“NoPIA”

C#5(VS 2012)

  • Asynchronous methods

  • Caller info attributes

C#6(VS 2015)

  • Draft Specification online

  • Compiler-as-a-service (Roslyn)

  • Import of static type members into namespace

  • Exception filters

  • Await in catch/finally blocks

  • Auto property initializers

  • Default values for getter-only properties

  • Expression-bodied members

  • Null propagator (null-conditional operator, succinct null checking)

  • String interpolation

  • nameof operator

  • Dictionary initializer

C#7.0(Visual Studio 2017)

  • Out variables

  • Pattern matching

  • Tuples

  • Deconstruction

  • Discards

  • Local Functions

  • Binary Literals

  • Digit Separators

  • Ref returns and locals

  • Generalized async return types

  • More expression-bodied members

  • Throw expressions

平臺(tái)依賴

大多數(shù)的 C# 8.0 功能都可以在任何版本的.NET 上運(yùn)行,但也有一些功能是有平臺(tái)依賴性的,例如異步流、范圍和索引都依賴 .NET Standard 2.1 一部分的新框架類型。其中,.NET Standard 2.1、.NET Core 3.0 以及 Xamarin,Unity 和 Mono 都將實(shí)現(xiàn) .NET Standard 2.1,而.NET?Framework 4.8 不會(huì),所以如果你使用的是 .NET Framework 4.8,那么 C# 8.0 的部分功能可能不能使用。

另外,接口成員的默認(rèn)實(shí)現(xiàn)也依賴新的運(yùn)行時(shí)增強(qiáng)功能,所以此功能也不適用于 .NET Framework 4.8 和舊版本的 .NET。

原文地址:https://blogs.msdn.microsoft.com/dotnet/2018/11/12/building-c-8-0/

.NET社區(qū)新聞,深度好文,歡迎訪問(wèn)公眾號(hào)文章匯總 http://www.csharpkit.com


總結(jié)

以上是生活随笔為你收集整理的VS 2019 要来了,是时候了解一下 C# 8.0 新功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。