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

How to use the new keyword in C # 8

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will talk to you about how to use the new keyword in C# 8. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something from this article.

On the face of it, this feature looks exactly the opposite of the var keyword. It does not infer the type of the variable from the expression, but the type of the expression from the variable. The following is an example of a comparison between the current syntax and the suggested syntax used by the proposal.

Dictionary field = new Dictionary (); Dictionary field = new ()

As you can see, there is no need to specify a type when it can infer a type from the context. It may not be very useful for local variables, but it is convenient for temporary parameters.

XmlReader.Create (reader, new XmlReaderSettings () {

IgnoreWhitespace = true})

XmlReader.Create (reader, new () {

IgnoreWhitespace = true})

In the above code, the XmlReaderSettings class is only used to hold the option values passed to XmlReader. We can even say that the type name of XmlReaderSettings is a bit "guest-oriented" for the important information it holds.

Another opportunity to use this feature is that you cannot use the fields and properties of the var keyword. For example:

Private readonly static object s_syncObj = new ()

According to the proposal, this feature cannot be used in four places.

Enumerated types: not all enumerated types contain constant zeros, so explicit enumerated members should be used.

Interface type: this is a niche feature, and it is best to specify the type explicitly.

Array type: arrays require special syntax to represent length.

Structure default constructor: it excludes all basic types and most value types. If you want to use the default value of this type, you can write it yourself.

After reading the above, do you have any further understanding of how to use the new keyword in C # 8? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report