This article is a mirror article of machine translation, please click here to jump to the original article.

View: 30894|Reply: 5

[Source] Explanation of C# 8.0 new features and knowledge points

[Copy link]
Posted on 2020-8-22 09:56:22 | | | |
.NET Core 3.x and .NET Standard 2.1 support C# 8.0 syntax, introduce many functional features, and add new feature syntax: Readonly members, default interface methods, pattern matching enhancements, attribute patterns, Tuple patterns, position patterns, switch expressions, using declarations, static local functions, asynchronous flows, indexes and ranges, null merge assignments, etc. This article will explain the new knowledge points of C# 8.0.

Index and scope

The following .NET types support both indexes and ranges: Array, String, Span, and ReadOnlySpan. List supports indexes, but not ranges



Example 1: The birthday of the person who obtained the ID number



Example 2: Get the contents of the last bit of the string



Example 3: Remove the last bit



switch

expression



Attribute mode



Tuple pattern



Location mode

Some types include a Deconstruct method that deconstructs its properties into discrete variables. If you have access to the Deconstruct method, you can use Position Patterns to check the properties of an object and use those properties for patterns. Consider the following Point class that contains a Deconstruct method for creating discrete variables for X and Y:



Also, consider the following enumeration of the various positions that represent the quadrant:



The following method uses the position pattern to extract the values of x and y. It then uses the when clause to determine the quadrant for that point:




Null merge assignments



Constructor expressions



using statement

The using declaration is a variable declaration preceded by the using keyword. It instructs the compiler to declare variables to be handled at the end of the closed scope. For example, the code for writing a text file is as follows:



In the previous example, the file is processed when the right parentheses of the method are reached. This is the end of the range of the declared file. The preceding code is equivalent to the following code using the classic using statement:



In the previous example, the file is processed when the right parentheses associated with the using statement are reached. In both cases, the compiler will generate a call to Dispose(). If the expression in the using statement is not available, the compiler will generate an error.

Resources

The hyperlink login is visible.

The hyperlink login is visible.




Previous:.NET/C# Use the WebClient download to complete the file name
Next:ASP.NET MVC5 is released using precompiled mode, improving page load speed and performance
 Landlord| Posted on 2022-7-11 08:38:51 |
[Turn]. NET 6 New Feature Enumerable.Chunk()
https://www.itsvse.com/thread-10286-1-1.html
Posted on 2023-3-21 10:02:24 |
Absorb something
Posted on 2023-3-23 09:11:01 |
Learning and learning
 Landlord| Posted on 2023-11-6 19:10:37 |
Index and scope



The result is as follows:



 Landlord| Posted on 2025-7-10 16:03:12 |
C# 8.0 syntax uses index and range
https://www.itsvse.com/thread-11003-1-1.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com