C#9.0推出的4個(gè)新特性介紹
在 .NET 5.0 的發(fā)布中,不僅統(tǒng)一了框架,微軟還在C#9.0中推出了一些新特性。
本版本中,印象深刻的功能:
- Init-only setters (初始化設(shè)置器)
- Records (記錄)
- Top-level statements (頂級(jí)語句)
- Pattern matching (模式匹配)
Init-only setters (初始化設(shè)置器)
以前,使用不可變數(shù)據(jù)實(shí)例化對(duì)象必須在構(gòu)造函數(shù)中通過將值作為參數(shù)傳遞來完成?,F(xiàn)在,它已被簡化為使用語法 init。它在對(duì)象創(chuàng)建期間初始化不可變數(shù)據(jù),這允許開發(fā)人員創(chuàng)建不可變屬性。
參考常規(guī)代碼:
class Customers { public int CustomerId { get; } public string CustomerName { get; set; } public Customers(int customerId) { CustomerId = customerId; } static void Main(string[] args) { var customers = new Customers(1045) { CustomerName = "Tyson" }; //customerid 不能設(shè)置,因?yàn)樵搶傩允侵蛔x customers.CustomerId = 1099; } }
使用 Init-only setters:
class Customers { public int CustomerId { get; init; } public string CustomerName { get; set; } static void Main(string[] args) { var customers = new Customers() { CustomerId = 1045, CustomerName = "Tyson" }; //CS8852:只能在對(duì)象初始值設(shè)定項(xiàng)中或在實(shí)例構(gòu)造函數(shù)或...分配 customers.CustomerId = 1099; } }
Records (記錄)
記錄允許我們像處理值而不是屬性集合一樣處理對(duì)象。由于記錄主要處理不可變狀態(tài),因此它們很靈活,也最適合用于數(shù)據(jù)而不是功能。
在以下示例中,我使用 with 表達(dá)式創(chuàng)建了一個(gè)新記錄,該記錄從另一個(gè)記錄繼承值。
參考常規(guī)代碼:
class SalesOrder { public int OrderId { get; init; } public string ProductName { get; init; } public int Quantity { get; init; } static void Main(string[] args) { SalesOrder order = new SalesOrder { OrderId = 1, ProductName = "Mobile", Quantity = 2 }; //修改ProductName SalesOrder newOrder = new SalesOrder { OrderId = order.OrderId, ProductName = "Laptop", Quantity = order.Quantity }; } }
使用 Records:
public record SalesOrder { public int OrderId { get; init; } public string ProductName { get; init; } public int Quantity { get; init; } static void Main(string[] args) { SalesOrder order = new SalesOrder { OrderId = 1, ProductName = "Mobile", Quantity = 2 }; SalesOrder newOrder = order with { ProductName = "Laptop" }; } }
Top-level statements (頂級(jí)語句)
此功能可幫助軟件開發(fā)人員從程序中排除不需要的代碼。頂級(jí)語句可以用一行替換所有重復(fù)代碼。
參考常規(guī)代碼:
using System; namespace CSharp9 { class Program { static void Main(string[] args) { Console.WriteLine("Welcome!"); } } }
使用 top-level statements:
using System; Console.WriteLine("Welcome !");
更準(zhǔn)確地說,我們可以使用:
System.Console.WriteLine("Welcome !");
Pattern matching (模式匹配)
C# 9.0 包含許多新模式,但在這里我們將討論關(guān)系模式和邏輯模式。
關(guān)系模式
這些模式與諸如 <、<=、> 和 >= 之類的關(guān)系運(yùn)算符一起使用。邏輯模式
這些模式與邏輯運(yùn)算符如 and、or 和 not 一起使用。
參考代碼:
public class SalesOrder { public int OrderId { get; set; } public string ProductName { get; set; } public int Quantity { get; set; } public int TotalCost { get; set; } public double GetTotalCost() => TotalCost switch { 500 or 600 => 10, < 1000 => 10 * 1.5, <= 10000 => 10 * 3, _ => 10 * 5 }; } class CSharpFeatures { static void Main(string[] args) { SalesOrder newOrderforCustomer1 = new SalesOrder() { OrderId = 1, ProductName = "Camera", Quantity = 1, TotalCost = 5000 }; newOrderforCustomer1.GetTotalCost(); SalesOrder newOrderforCustomer2 = new SalesOrder() { OrderId = 2, ProductName = "Pen", Quantity = 1, TotalCost = 500 }; newOrderforCustomer2.GetTotalCost(); } }
結(jié)論
借助這些功能,C# 9.0 可幫助程序員輕松處理數(shù)據(jù)(記錄)、形狀代碼(模式匹配)和簡化代碼(頂級(jí)語句)。
如果想了解更多關(guān)于 C# 9.0 正式版中的新功能,請閱讀此文檔。
以上所述是小編給大家介紹的C#9.0推出的4個(gè)新特性,希望對(duì)大家有所幫助。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 關(guān)于C#10 新特性 Lambda 優(yōu)化
- C# 9 新特性——record的相關(guān)總結(jié)
- C#9新特性之增強(qiáng)的模式匹配
- C#9新特性init only setter的使用
- C# 9 新特性之增強(qiáng)的foreach詳解
- C#9.0新特性詳解——頂級(jí)程序語句(Top-Level Programs)
- c# 9.0新特性——模塊初始化器
- C# 9.0新特性——擴(kuò)展方法GetEnumerator支持foreach循環(huán)
- C# 9.0新特性——只初始化設(shè)置器
- 淺析C# 9.0 新特性之 Lambda 棄元參數(shù)
- 淺談C# 9.0 新特性之只讀屬性和記錄
相關(guān)文章
C#實(shí)現(xiàn)漢字轉(zhuǎn)換為拼音縮寫的代碼
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)漢字轉(zhuǎn)換為拼音縮寫的代碼,感興趣的小伙伴們可以參考一下2016-07-07幾分鐘搞懂c#之FileStream對(duì)象讀寫大文件(推薦)
這篇文章主要介紹了c#之FileStream對(duì)象讀寫大文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04c#使用ManagedWifi查看當(dāng)前Wifi信號(hào)并選擇wifi的示例
這篇文章主要介紹了c#使用ManagedWifi查看當(dāng)前Wifi信號(hào)并選擇wifi的示例,需要的朋友可以參考下2014-04-04C#實(shí)現(xiàn)設(shè)置電腦顯示器參數(shù)
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)設(shè)置電腦顯示器參數(shù),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12使用C#實(shí)現(xiàn)基于TCP和UDP協(xié)議的網(wǎng)絡(luò)通信程序的基本示例
這篇文章主要介紹了使用C#實(shí)現(xiàn)基于TCP和UDP協(xié)議的網(wǎng)絡(luò)通信程序的示例,文中分別編寫了基本的服務(wù)器端和客戶端,代碼十分簡單,需要的朋友可以參考下2016-04-04C# WinForm制作一個(gè)批量轉(zhuǎn)化文件格式的小工具
在生活中有時(shí)候會(huì)遇到批量轉(zhuǎn)換格式的需求,一個(gè)個(gè)點(diǎn)太麻煩了,一個(gè)能夠?qū)崿F(xiàn)批量文件格式轉(zhuǎn)換的工具非常有用,所以本文小編使用C# WinForm制作一個(gè)批量轉(zhuǎn)化文件格式的小工具,文中有具體實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-11-11