This is the seventh day of my participation in the More text Challenge. For details, see more text Challenge

After a long hiatus, the new feature series of C#8.0 has started again today, making a little progress every day

1. The traditional approach to array operations

For those of you who have learned about arrays, you know that arrays can be accessed directly by the index, so this is the most direct way to access an array.

If we need to access the last few letters of the array, we just need to get the total length of the array and subtract the number of letters from it.

Of course, c# also provides Linq queries. We can use the combination Skip, Take to get the letter of the specified position.

The example code is as follows:

string[] letters = { "A"."B"."C"."D"."E"."F"."G"."H" };
string firstLetter = letters[0];
string secondLetter = letters[1];
string lastLetter = letters[letters.Length - 1];
string penultimateLetter = letters[letters.Length - 2];

string[] middleTwoLetters = letters.Skip(2) 
                                   .Take(2) 
                                   .ToArray();

string[] everythingExceptFirstAndLast = letters.Skip(1) 
                                               .Take(letters.Length - 2) 
                                               .ToArray();

Console.WriteLine($"{firstLetter}.{secondLetter}.{lastLetter}.{penultimateLetter}");

Console.WriteLine($"middleTwoLetters:{string.Join(",",middleTwoLetters)}");
Console.WriteLine($"everythingExceptFirstAndLast:{string.Join(",", everythingExceptFirstAndLast)}");
Copy the code
A,B,H,G
middleTwoLetters:C,D
everythingExceptFirstAndLast:B,C,D,E,F,G
Copy the code

2. C# introduces ^, finding elements backwards

^ N, where N is the length of the array, so ^1 is the last reciprocal.

For example, the above code can be simplified as follows:

string[] letters = { "A"."B"."C"."D"."E"."F"."G"."H" };
string firstLetter = letters[0];
string secondLetter = letters[1];
string lastLetter = letters[^1];
string penultimateLetter = letters[^2];

string[] middleTwoLetters = letters[2.4.];

string[] everythingExceptFirstAndLast = letters[1.. ^1];

Console.WriteLine($"{firstLetter}.{secondLetter}.{lastLetter}.{penultimateLetter}");

Console.WriteLine($"middleTwoLetters:{string.Join(",",middleTwoLetters)}");
Console.WriteLine($"everythingExceptFirstAndLast:{string.Join(",", everythingExceptFirstAndLast)}");
Copy the code

Print the following results

A,B,H,G
middleTwoLetters:C,D
everythingExceptFirstAndLast:B,C,D,E,F,G
Copy the code

In addition to the newly introduced ^N index symbol, the scope operator.. For example, [2..4] in the code gives us the letters C and D, or, in other words, it gives us a range of elements starting at 2 and ending at 4.

Note that the last index is not inclusive, meaning that it contains a range of 2 but not 4.

Of course, you can use open scope, meaning until the beginning or end, for example: [^3..] [.. 5]

To make the new indexing feature work, C# has introduced a new structure for Index, as well as a new structure for Range to be compatible with scope scopes.

You can define code directly using these two constructs.

Index index = new Index(5.true);
string tmp = letters[index];

Range middleTwo = new Range(2.4);
string[] tmp2 = letters[middleTwo];
Copy the code

Of course, you can also mix the end operator within the range operator:

string[] lastThreeLetters = letters[^3.. ^0]; //E,F,G
string tmp3 = letters[^0]; //IndexOutOfRangeException
Copy the code

Here, the first ^0 represents the end of the array, because the end of the range is not included, so there are no exceptions; However, as an index, an IndexOutOfRangeException is generated.

3. Summary

Of course, some new types also support inverted indexes and ranges, but the specific classes need to refer to the specific documentation, which will not be covered here.

After reading this short article, have you learned another way to simplify your code?

Thanks for your company, I am not alone on the road of technology ~~

I’m a Webmote, pay attention to me, don’t get lost on the way to study!