Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the new features of Craft 3.5?

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what are the new features of Craft 3.5, which can be used for reference. I hope you can learn a lot after reading this article. Let's take a look at it.

I. what's new in C # 3.5-the var keyword

Does the keyword var remind you of the keyword var in JavaScript that defines variables? In fact, these two seemingly identical keywords are essentially different. This var keyword does not appear in C # 3.5, it already exists in C # 3.0. The reason for introducing it here is that to introduce 3.5, we must and should introduce this feature that appears in C # 3.0-native type inference (Local Type Reference).

In Category 2.0 and its previous versions, if you define a variable that can assign any value to it, then we need to define it with the object keyword, which requires the boxing operation of the value type and the corresponding unpacking operation when using the variable, and the resources consumed by this kind of unpacking and unpacking are really not what I want to see.

So what if we don't want to write unnecessary resource-consuming code and want to implement the function of object defining variables? Clocking 3.5 provides us with a way to define variables:

Var intTest = 5; var customer = new Customer ()

If you look at the variable definition above, this is actually a new feature that Category 3.5 provides for us-native type inference, which protects type safety and allows you to write more "free" code. In other words, we can modify the variable with the var keyword without considering the type of variable, and the compiler can intelligently infer the type of variable from the expression that assigns the value to the variable. It is completely different from the variables defined by the Variant keyword in the COM model. The Variant keyword in COM is a way of late variable binding, which is not detected at compile time, only when the code is running, and there will be a lot of annoying Bug if you are not careful. The variable defined by the var in C # infers its type at compile time, and the compiled IL code contains only the inferred type.

That is, the above two lines of code are exactly equivalent to the following code:

Var intTest = 5; Customer customer = new Customer ()

In fact, the var keyword will not only do what we have described above, but it will become a friend you are very familiar with when programming with Category 3.5, remembering this "anonymous type (Anonymous Type)" as well as this "native type inference (Local Type Reference)".

II. What's new in C # 3.5-automatic attributes (Automatic Properties)

Remember that small private variable that we defined when we wrote the properties of the class in Category 1.1 and Category 2.0? Some people even argue about whether private variables should be defined with attributes. Then, the popular word "losing weight" in the current society can also be applied to our program code.

Automatic Properties 3.5 provides us with automatic attributes, as shown in the following code:

Var intTest = 5; var customer = new Customer ()

The get and set keywords are not as familiar as return value; and _ privateField=value;. In fact, this is another new feature that the considerate compiler provides for us-automatic properties. The compiler automatically generates a private variable for the Name property we define to hold its value. As a result, the code that we used to need at least three lines to complete is now easily completed in one line.

However, it has its own limitations, such as not adding logical judgment to attributes defined by automatic attributes, get and set must appear in pairs, and so on. However, the author believes that not all of our attributes should be added with logical judgment, right? So, please try the convenience that automatic attributes bring to us

III. What's New in C # 3.5-lambda expression

Accustomed to C # programming, have you ever seen the symbol "= >" in Craft 2.0 or before? This is another new feature that we can read as a "lambda expression".

C # 2.0 generic uses anonymous methods to introduce the ability to pass pointers to specific code as parameters. This is a powerful concept, but in this way you are actually passing a pointer to a method, not a code block. That reference points to the strongly typed code generated at compile time. With generics, you can gain more flexibility, but it is difficult to apply standard operators to generic types. C # 3.0 introduces lambda expressions, which allow anonymous methods to be defined using a more concise syntax.

Look at the following code snippet:

IEnumerbleHezeCstomers = customers wheren (c = > .Address = = City.here Select (c = > C); l

Not to mention the rest of the meaning, just look at our .where parentheses, c = > c.Address==City.Heze this code, we can understand that given c, returns the recordset of c.Address==City.Heze, here is one of the applications of lambda expressions.

Thank you for reading this article carefully. I hope the article "what are the new features of Craft 3.5" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report