What are the features of C# 10
Editor to share with you what are the characteristics of C# 10, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Constant interpolated string
C # 10 allows the use of interpolation in constant string initialization, as follows
Const string name = "Oleg"; const string greeting = $"Hello, {name}."; Console.WriteLine (greeting); / / Output: Hello, Oleg. Extended attribute mode
Starting with C # 10, you can reference nested properties or fields in the appropriate schema, which becomes more readable and requires fewer curly braces.
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;}}
If Location is null, the pattern is not matched and false is returned.
Namespace of the file scope
C # 10 introduces a new way of declaring namespaces-file-wide namespaces, reducing curly braces and making the code structure more concise.
Namespace FileScopedNamespace;class Program {static void Main (string [] args) {Console.WriteLine ("Hello World!");}} Global Using
One reference, universal
Global using System;global using System.Collections.Generic;global using System.Linq;global using System.Threading.Tasks;List list = new () {1,2,3,4}; int sum = list.Sum (); Console.WriteLine (sum); await Task.Delay (1000); assignment and declaration in the same deconstruction
C # 10 can be assigned and declared in the same deconstruction.
Var rgb = (255,100,30); / / Initialization & assignmentint r; (r, int g, int b) = rgb;Console.WriteLine ($"RGB: {r}, {g}, {b}"); / / Output: RGB: 255,100, 30Record type rewriting ToString () supports sealing Product product = new () {Name = "Bread"}; Console.WriteLine (product.ToString ()); / / Output: Breadpublic record Product {public string Name {get; init () } public sealed override string ToString () {return Name;}} Record Struct
C# 10 supports 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"}; Console.WriteLine (me = = anotherMe); / / Output: Output {Truerecord struct Person {Truerecord struct Person } public string LastName {get; init;}} record struct Product (string Name, decimal Price); Struct field supports initialization using System;Person person = new () {Name = "Oleg"}; Console.WriteLine (person.Id + "" + person.Name); / / Output: 0cc6caac-d061-4f46-9301-c7cc2a012e47 Olegstruct Person {public Guid Id {get; init;} = Guid.NewGuid (); Attributes support for public string Name {get; set;} Lambda expression
C # 9 supports Attributes for native functions, while C # 10 adds Attributes support for Lambda expressions.
Action a = [MyAttribute] () = > {}; Action b = [return: MyAttribute] (x) = > {}; Action c = [MyAttribute] ([MyAttribute] x) = > {}; class MyAttribute: explicit return type Test () in Attribute {} Lambda; var L1 = string () = > string.Empty;var L2 = int () = > 0X var L3 = static void () = > {}; void Test () {var L4 = T () = > default } AsyncMethodBuilder properties applied to the method
Starting with C # 7, you can only apply the AsyncMethodBuilder feature to a type, and in C # 10, you can also apply it to a single method.
Expressions in the structure of using System.Runtime.CompilerServices;class Example {[AsyncMethodBuilder (typeof (AsyncVoidMethodBuilder))] public void ExampleMethod () {}}
C # 10 supports using with expressions with struct
Product potato = new () {Name = "Potato", Category = "Vegetable"}; Console.WriteLine ($"{potato.Name} {potato.Category}"); / / Output: Potato VegetableProduct tomato = potato with {Name = "Tomato"}; Console.WriteLine ($"{tomato.Name} {tomato.Category}"); / / Output: Tomato Vegetablestruct Product {public string Name {get; set;} public string Category {get; set;}
Expressions in anonymous types
C # 10 supports using with expressions with anonymous types
Var potato = new {Name = "Potato", Category = "Vegetable"}; Console.WriteLine ($"{potato.Name} {potato.Category}"); / / Output: Potato Vegetablevar onion = potato with {Name = "Onion"}; Console.WriteLine ($"{onion.Name} {onion.Category}"); / / Output: Onion Vegetable is all about the article "what are the features of C# 10"? thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!