In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "summary of new features in Craft 6.0". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the summary of new features in Craft 6.0".
1.Getter proprietary automatic feature
Previously, automatic attributes must have set, which will be disadvantageous to immutable variables, so automatic properties with only get are allowed in C # 6.0. the compiler will recognize this property as read-only, even if there is no set, we can still assign values to the property from the constructor. This assignment process can be achieved without set, which is directly assigned to the supported field in order to initialize it. As shown in the following code.
Using System;namespace TheNewCSharp6._0 {/ / Getter exclusive automatic new feature public class Point {public int X {get;} public int Y {get;} public Point (int x, int y) {X = x; Y = y;} public double Dist () {return Math.Sqrt (X * X + Y * Y);}
two。 Use static members
A new using clause is introduced in C # 6.0. it is a reference type, not a namespace, so that the static members of that type can be put directly into scope. For example, if we want to use the Sqrt function in the previous example, we must add the math prefix to call the Sqrt (square root) method.
When we add
Using static System.Math
You can use the Sqrt method without adding the math prefix, as shown in the following code:
Using static System.Math;namespace TheNewCSharp6._0 {/ / uses static member public class Point1 {public int X {get;} public int Y {get;} public Point1 (int x, int y) {X = x; Y = y;} public double Dist () {return Sqrt (X * X + Y * Y);}
We have always thought that Monday is definitely a day of the week, and yellow is one of the colors. It doesn't make much sense to state the end-result of the method every time in your code, and this new feature just overcomes this difficulty.
3. String interpolation
String.Format is a very useful and powerful API, but it is huge, and placeholder and number-related problems can make people feel confused, error-prone, and disturb our intentions. It would be better if the value you want to format appears in the appropriate place, which is what this string interpolation syntax is for:
Public override string ToString () {return $"({X}, {Y})";}
The call to String.Format disappears, adding a dollar sign to indicate that this is an interpolated string. Then delete the placeholder number, leave some space, and put the expression to be formatted in the corresponding space, so that it looks clear and concise.
4. Expression aspect method
For many methods, there is only a simple return statement in the subject, and we can replace it with an lambda expression (instead of the statement body).
This also applies to other types of function members. For addition, it is a get with a single return statement, which is much more concise than two curly braces.
Here, we can write the entire code like this, with an expression and an arrow without a get keyword, thus compressing the code to make the code more compact.
Using static System.Math;namespace TheNewCSharp6._0 {/ / expression body attribute public class Point3 {public int X {get;} public int Y {get;} public Point3 (int x, int y) {X = x; Y = y;} public double Dist = > Sqrt (X * X + Y * Y); public override string ToString () = > $"({X}, {Y})";}}
5. Index initializer
/ / before public JObject ToJsonOld () {var result = new JObject (); result ["x"] = X; result ["y"] = Y; return result;}
This is a way to convert a point object into a JSON object, through which you can initialize the properties in the object initializer.
With C # 6.0, we can use the square bracket syntax that is obvious here to assign to the internal index.
So you can initialize the json object in an expression, as follows:
/ / After public JObject ToJsonNew () = > new JObject () {["x"] = X, ["y"] = Y}
6 Null conditional operator
In the above example, we can create and assign a json object in one line of code, but we need to check the object before using it. In most cases, we mainly check whether the object is empty, as shown below:
/ / before public static Point FromJson (JObject json) {if (json! = null & & json ["x"]! = null & & json ["x"]. Type = = JTokenType.Integer & & json ["y"]! = null & & json ["y"]. Type = = JTokenType.Integer) {return new Point ((int) json ["x"], (int) json ["y"]);} return null;}
We need to check that it is not empty before using it, and then ensure that the index result is not empty, and then check the value type after ensuring that it can be accessed.
The Null conditional operator is mainly used here to handle the entire null check.
The following is the result after we removed the display null check. The null judgment is changed to the question point operator. Here's how it works: if the left side is null, then return null, if it's not null, then we can execute. The operation to the right of the number.
/ / After public static Point FromJson1 (JObject json) {if (json! = null & & json ["x"]? .Type = = JTokenType.Integer & & json ["y"]? .Type = = JTokenType.Integer) {return new Point ((int) json ["x"], (int) json ["y"]);} return null;}
If null, then null.
If not null, then execute.
We can also use the question point operator in succession, which is simplified as follows:
/ / finally public static Point FromJson2 (JObject json) {if (json? ["x"]? .Type = = JTokenType.Integer & & json? ["y"]? .Type = = JTokenType.Integer) {return new Point ((int) json ["x"], (int) json ["y"]);} return null;}
In this way, this if condition only expresses your core intention and does not have to spend too much code on null judgment.
The Null conditional operator is very useful for trigger time, such as
OnChanged?.Invoke (this,arg)
Instead of determining whether the delegate is empty alone, execute the Invoke () method on the right when the delegate is not empty.
7.Nameof operator
In many cases, we need to get the name of the program element in the form of an operator, and the Nameof operator allows us to get a string of the name of the element, know exactly what the element refers to, which elements are, and make sure it does exist.
Public void Add (Point point) {if (point==null) throw new ArgumentNullException (nameof (point);}
8. Abnormal plug selector
The exception plug selector allows catch to filter the exception before capturing the exception. If the exception meets our requirements, the exception filter is as follows:
Try {...} catch (ConfigurationException e) when (e.IsSevere) {...}
9. Using await in catch and finally
More and more api are asynchronous, and now we can finally call them in catch and finally.
Thank you for your reading. The above is the content of "Summary of the New Features in Craft 6.0". After the study of this article, I believe you have a deeper understanding of the summary of the new features in Craft 6.0, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.