您好,登錄后才能下訂單哦!
小編給大家分享一下C# 10的特性有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
C# 10 允許使用在常量字符串初始化中使用插值, 如下
const string name = "Oleg"; const string greeting = $"Hello, {name}."; Console.WriteLine(greeting); // Output: Hello, Oleg.
從 C# 10 開始,您可以在適當的模式中引用嵌套的屬性或字段, 屬性模式變得更具可讀性并且需要更少的大括號。
Person person = new() { Name = "Oleg", Location = new() { Country = "PL" } }; if (person is { Name: "Oleg", Location.Country: "PL" }) { Console.WriteLine("It's me!"); } class Person { public string Name { get; set; } public Location Location { get; set; } } class Location { public string Country { get; set; } }
如果Location為null,則不會匹配模式并返回false。
C# 10 引入了一種新的命名空間聲明方式 - 文件范圍的命名空間,減少一個大括號,代碼結構更簡潔。
namespace FileScopedNamespace; class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); } }
一次引用,全局通用
global using System; global using System.Collections.Generic; global using System.Linq; global using System.Threading.Tasks; List<int> list = new() { 1, 2, 3, 4 }; int sum = list.Sum(); Console.WriteLine(sum); await Task.Delay(1000);
C# 10 可以在同一個解構中進行賦值和聲明。
var rgb = (255, 100, 30); // Initialization & assignment int r; (r, int g, int b) = rgb; Console.WriteLine($"RGB: {r}, {g}, {b}"); // Output: RGB: 255, 100, 30
Product product = new() { Name = "Bread" }; Console.WriteLine(product.ToString()); // Output: Bread public record Product { public string Name { get; init; } public sealed override string ToString() { return Name; } }
C# 10 支持 record struct
Person me = new() { FirstName = "Oleg", LastName = "Kyrylchuk" }; Console.WriteLine(me); // Output: Person { FirstName = Oleg, LastName = Kyrylchuk } Person otherPerson = me with { FirstName = "John" }; Console.WriteLine(otherPerson); // Output: Person { FirstName = John, LastName = Kyrylchuk } Person anotherMe = new() { FirstName = "Oleg", LastName = "Kyrylchuk" }; C onsole.WriteLine(me == anotherMe); // Output: True record struct Person { public string FirstName { get; init; } public string LastName { get; init; } } record struct Product(string Name, decimal Price);
using System; Person person = new() { Name = "Oleg" }; Console.WriteLine(person.Id + " " + person.Name); // Output: 0cc6caac-d061-4f46-9301-c7cc2a012e47 Oleg struct Person { public Guid Id { get; init; } = Guid.NewGuid(); public string Name { get; set; } }
C# 9 支持本地函數的 Attributes, C# 10 添加了 Lambda 表達式的 Attributes 支持。
Action a = [MyAttribute] () => { }; Action<int> b =[return: MyAttribute] (x) => { }; Action<int> c =[MyAttribute] ([MyAttribute] x) => { }; class MyAttribute : Attribute { }
Test<int>(); var l1 = string () => string.Empty; var l2 = int () => 0; var l3 = static void () => { }; void Test<T>() { var l4 = T () => default; }
從 C# 7 開始,您只能將AsyncMethodBuilder 特性應用于類型, 在 C# 10 中,您還可以將該特性應用于單個方法。
using System.Runtime.CompilerServices; class Example { [AsyncMethodBuilder(typeof(AsyncVoidMethodBuilder))] public void ExampleMethod() { } }
C# 10 支持 將 with 表達式和 struct 一起使用
Product potato = new() { Name = "Potato", Category = "Vegetable" }; Console.WriteLine($"{potato.Name} {potato.Category}"); // Output: Potato Vegetable Product tomato = potato with { Name = "Tomato" }; Console.WriteLine($"{tomato.Name} {tomato.Category}"); // Output: Tomato Vegetable struct Product { public string Name { get; set; } public string Category { get; set; } }
匿名類型中的表達式
C# 10 支持 將 with 表達式和匿名類型一起使用
var potato = new { Name = "Potato", Category = "Vegetable" }; Console.WriteLine($"{potato.Name} {potato.Category}"); // Output: Potato Vegetable var onion = potato with { Name = "Onion" }; Console.WriteLine($"{onion.Name} {onion.Category}"); // Output: Onion Vegetable
以上是“C# 10的特性有哪些”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。