網站首頁 編程語言 正文
在 .NET 5.0 的發布中,不僅統一了框架,微軟還在C#9.0中推出了一些新特性。
本版本中,印象深刻的功能:
- Init-only setters (初始化設置器)
- Records (記錄)
- Top-level statements (頂級語句)
- Pattern matching (模式匹配)
Init-only setters (初始化設置器)
以前,使用不可變數據實例化對象必須在構造函數中通過將值作為參數傳遞來完成。現在,它已被簡化為使用語法 init。它在對象創建期間初始化不可變數據,這允許開發人員創建不可變屬性。
參考常規代碼:
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 不能設置,因為該屬性是只讀 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:只能在對象初始值設定項中或在實例構造函數或...分配 customers.CustomerId = 1099; } }
Records (記錄)
記錄允許我們像處理值而不是屬性集合一樣處理對象。由于記錄主要處理不可變狀態,因此它們很靈活,也最適合用于數據而不是功能。
在以下示例中,我使用 with 表達式創建了一個新記錄,該記錄從另一個記錄繼承值。
參考常規代碼:
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 (頂級語句)
此功能可幫助軟件開發人員從程序中排除不需要的代碼。頂級語句可以用一行替換所有重復代碼。
參考常規代碼:
using System; namespace CSharp9 { class Program { static void Main(string[] args) { Console.WriteLine("Welcome!"); } } }
使用 top-level statements:
using System; Console.WriteLine("Welcome !");
更準確地說,我們可以使用:
System.Console.WriteLine("Welcome !");
Pattern matching (模式匹配)
C# 9.0 包含許多新模式,但在這里我們將討論關系模式和邏輯模式。
關系模式
這些模式與諸如 <、<=、> 和 >= 之類的關系運算符一起使用。邏輯模式
這些模式與邏輯運算符如 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(); } }
結論
借助這些功能,C# 9.0 可幫助程序員輕松處理數據(記錄)、形狀代碼(模式匹配)和簡化代碼(頂級語句)。
如果想了解更多關于 C# 9.0 正式版中的新功能,請閱讀此文檔。
原文鏈接:https://www.cnblogs.com/zh7791/p/15222578.html
相關推薦
- 2022-11-10 Android自定義DataTimePicker日期時間選擇器使用詳解_Android
- 2022-12-19 批處理bat腳本獲取打包發布問題記錄_DOS/BAT
- 2022-11-03 iOS開發xconfig和script腳本使用詳解_IOS
- 2023-02-15 Qt槽函數會被執行多次的問題原因及解決方法_C 語言
- 2022-10-26 一文解析?Golang?sync.Once?用法及原理_Golang
- 2022-04-14 zsh: command not found:快速的解決方法
- 2023-05-15 bash?if條件判斷的使用_linux shell
- 2023-06-19 C++單一職責原則示例代碼淺析_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支