In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what are the new features of Craft 10". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the new features of Craft 10.
1 、 Natural types for lambdas
C # 10 can do type inference better. In many cases, the compiler can infer types automatically, so we no longer have to explicitly declare delegate types.
Let's look at a simple example:
/ / Func func = () = > 1 var func = () = > 1 / Func func2 = () = > "Hello"; var func2 = () = > "Hello"
In previous versions, we need to explicitly declare the delegate type, such as the above annotated code. In C # 10, we can directly use var to declare that the delegate type is inferred by the compiler.
Some methods may be overloaded and support different parameter types. The delegate type cannot be directly inferred by compilation. We can specify the input parameter type. The example is as follows:
/ / Func parse = (string s) = > int.Parse (s); var parse = (string s) = > int.Parse (s); 2. Lambda Ref/Out/In Parameter Modifier
When we specify the input parameter type, we can set ref / out / int to represent the reference passing of a value type
Examples are as follows:
Var refFunc = (ref int x) = > {x =-1;}; var outFunc = (out int x) = > {x =-1;}; var inFunc = (in int x) = > {}; var num = 1 num (ref num); Console.WriteLine (num); outFunc (out num); Console.WriteLine (num)
The output is 2 and-1, respectively.
3 、 Lambda Return Type
The delegate of C # 10 can specify the return type, so it may be more convenient for us to return the delegate.
For example, the following example:
/ return typevar lambdaWithReturnValue0 = int? () = > null;// return type and input typevar lambdaWithReturnValue1 = int? (string s) = > string.IsNullOrEmpty (s)? 1: null;// Funcvar choose = object (bool b) = > b? 1: "two"
In this way, there is no need to explicitly declare the delegate type, and further help the compiler to make inferences on the basis of the above
4 、 Natual types for method
For methods that can infer the type, we can also use var to declare the delegate
Examples are as follows:
/ / Action func3 = LocalMethod;var func3 = LocalMethod;void LocalMethod (string a) {Console.WriteLine (a);} var checkFunc = string.IsNullOrEmpty;var read = Console.Read;Action write = Console.Write
For those who cannot infer the type, it is necessary to explicitly declare the type delegate type. For example, the parameter of the last delegate Console.Write above will have multiple overloads, so the type cannot be accurately inferred, so the delegate type needs to be declared.
5 、 Lambda Attribute
Now we can specify Attribute in the Lambda expression
Var parse3 = [Description ("Lambda attribute")] (string s) = > int.Parse (s); var choose3 = [Description ("Lambda attribute1")] object (bool b) = > b? 1: "two"
This is also used in ASP.NET Core Minimal API, such as:
App.MapPost ("/ todo", [Authorize] () = > "Success")
Use ILSpy to view the lower version of C# code, and the generated code is as follows:
[Description ("Lambda attribute")] internal int baggage 4: 0 (string s) {return int.Parse (s);} [Description ("Lambda attribute1")] internal object breadcrumb 4: 1 (bool b) {return b? ((object) 1): "two";} 6, More
These Lambda optimizations can make it easier for us to use lambda, and there are many applications in ASP.NET Core.
App.Map ("/", () = > "Hello world"); app.MapPost ("/ auth", [Authorize] () = > "Authorize needed")
It can be automatically converted to Expression when declaring Lambda, or we can declare it in combination with the new features above
Examples are as follows:
Expression expr = (string s) = > int.Parse (s); LambdaExpression parseExpr = object (bool b) = > b? 1: "two"; Expression parseExpr1 = int? () = > null; so far, I believe you have a deeper understanding of "what are the new features of Category 10". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.