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

View: 5151|Reply: 3

[Source] C# 8.0 Syntax Features: New Operator System Index

[Copy link]
Posted on 7/11/2022 7:35:19 PM | | | |
Requirements: To define a collection, we need to get the last element of the collection, the code is usually array[array. Length - 1] In this way, C# 8.0 introduces a new index expression meaning "from the last". The feature will introduce a new unary prefix "hat" operator.

review

Explanation of C# 8.0 new features and knowledge points
https://www.itsvse.com/thread-9355-1-1.html

[Turn]. NET 6 New Feature Enumerable.Chunk()
https://www.itsvse.com/thread-10286-1-1.html
System index

C# can't index a collection from the end, but most indexers use the concept of "start with", or execute the "length-i" expression. We introduced a new index expression that means "from the end". The feature will introduce a new unary prefix "hat" operator. Its single operand must be convertible to System.Int32. It will be reduced to the appropriate System.Index factory method call.


string[] words = new string[]
{
                // index from start    index from end
    "The",      // 0                   ^9
    "quick",    // 1                   ^8
    "brown",    // 2                   ^7
    "fox",      // 3                   ^6
    "jumps",    // 4                   ^5
    "over",     // 5                   ^4
    "the",      // 6                   ^3
    "lazy",     // 7                   ^2
    "dog"       // 8                   ^1
};              // 9 (or words. Length) ^0
With the new syntax, we can easily get the last element object of a collection with the following code:



The index is the same as 0 sequence[0]. The index is the same as ^0 sequence[sequence. Length]。 The expression sequence[^0] does throw an exception, just like sequence[sequence. Length]. For any number n, the index ^n is the samesequence. Length - n

Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.


Resources:

The hyperlink login is visible.
The hyperlink login is visible.


(End)




Previous:[Actual combat]. NET/C# uses UDP to send and accept data
Next:Use PowerShell to enter the local Group Policy Editor and modify and save
Posted on 7/13/2022 10:59:44 PM |
Learn a little...
Posted on 8/11/2022 3:16:06 PM |
I came to learn and hopefully the landlord could do it
 Landlord| Posted on 5/14/2025 4:11:17 PM |
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