Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

An example Analysis of the derivation of new expression for the Target Type of Category 9.0

Shulou Source: shulou.com Published: 2022-06-01 20:47:27 10月05日 Update

This article introduces the relevant knowledge of "deducing new expression for example analysis of target type 9.0". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

You must be familiar with type derivation, which introduced C # when the var keyword was introduced.

Var I = 10

Var u = new User ()

The compiler automatically deduces the type of the variable on the left from the literal quantity on the right, which can be summarized as follows: derive the type on the left from the right side of the context. We call it source type inference (Source-typed inferring, refer to the term coined by Target-typed).

Accordingly, active type derivation is the target type derivation (Target-typed inferring), which refers to the derivation of the type on the right from the left side of the context. For example, array initialization and Lambda expressions are often derived from the target type. For example:

/ / No type derivation is used

String [] s = new string [] {"a", "b"}

/ / derivation of target type (push left and right)

String [] s = new {"a", "b"}

String [] s = new [] {"a", "b"}

/ / No type derivation is used

Users.FirstOrDefault (u = > u.id = 123)

/ / derivation of target type (push left and right)

Users.FirstOrDefault (u = > u.id = 123)

This time in C # 9, the target type derivation of the user-defined type new expression is added, that is, the type of the new expression is automatically deduced from the left side of the context, thus omitting the type assignment when using the new construction. See the example:

/ / before C# 9

Point p = new Point (3,5)

/ / C# 9

Point p = new (3,5)

In addition, C# 9 also adds operators. Target type derivation support for and? The previous two operators must require that the operands on both sides are of the same type, otherwise an error will be compiled. In C# 9, compilation errors are no longer reported as long as the target type is the common base class of operands, such as:

/ / Student and Customer have a common parent class Person

Person person = (Person) (student? Customer); / / before C# 9

Person person = student? Customer; / / C# 9

/ / Null type. Both 0 and null can be implicitly converted to int? Types

Int? Result = b? 0: (int?) before null; / / C# 9

Int? Result = b? 0: null; / / C# 9

In fact, the core of this article is a sentence of code:

Point p = new (3,5); "example analysis of new expression derivation of target type 9.0" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Type caching target expression up and down context right compilation analysis content object operator more knowledge utility same learned and then two code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple Microsoft NVidia OPPO Reno