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

Usage scenarios of C # discarded meta-parameters

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "the use scene of C# abandoned meta-parameters". In the daily operation, I believe that many people have doubts about the use of C# abandoned meta-parameters. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about the use of C# meta-parameters! Next, please follow the editor to study!

Discards, which is a temporary virtual variable that is artificially discarded and not used, was first supported in C # 7. 0. Syntactically, it is used for assignment, but it is not allocated storage space, that is, there is no value, so it cannot be read from it. Discard elements are indicated by _ (underscore). An underscore is a keyword that can only be assigned and cannot be read, for example:

In C# 7.0, there are four main scenarios for the use of discarded elements:

Deconstruction of tuples and objects

Pattern matching using is and switch

Calls to methods with out parameters

Independent usage scenarios within scope

For these scenarios, use the following code to demonstrate.

Scenario 1: deconstruction of tuples / objects

Var tuple = (1,2,3,4,5)

(_, var fifth) = tuple

Scenario 2: pattern matching using is/switch

Var obj = CultureInfo.CurrentCulture.DateTimeFormat

Switch (obj)

{

Case IFormatProvider fmt:

Console.WriteLine ($"{fmt} object")

Break

Case null:

Console.Write ("A null object reference")

Break

Case object _:

Console.WriteLine ("Some object type without format information")

Break

}

If (obj is object _)

{

...

}

Scenario 3: invocation of a method with out parameters

Var point = new Point (10,10)

/ / as long as x, don't care about y

Point.GetCoordinates (out int x, out _)

Scenario 4: independent usage scenarios in scope

Void Test (Dto dto)

{

_ = dto? Throw new ArgumentNullException (nameof (dto))

}

With an understanding of the discarded element and the four usage scenarios of the discarded element, it is easy to understand the following scenario for the use of discarded elements, which is newly supported in C# 9.0.

C # 9.0 adds a scenario support for discards: Lambda parameters, as well as anonymous method parameters. Example:

/ / before C# 9

Func zero = (a, b) = > 0

Func func = delegate (int a, int b) {return 0;}

/ / C# 9

Func zero = (_, _) = > 0

Func func = delegate (int _, int _) {return 0;}

Prior to C # 9, even unused Lambda parameters needed to be named. C # 9 supports discard parameters to simplify naming on the one hand and save memory allocation on the other. More importantly, it makes the intention of programming clearer, makes it clear at a glance that this parameter is not used, and enhances the readability and maintainability of the code.

At this point, the study of "scenarios for the use of C# discarded meta-parameters" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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