Preface
The C# language was released in 2000 and has officially released 7 versions so far, each of which contains many exciting new features and feature updates. At the same time, the release of each version of C# is highly coupled with the same period Visual Studio and .NET runtime versions, which also helps developers better learn and master C# and combine it with the use of Visual Studio and .NET.
A new range type (Range Type) has been added to C# 8.0.
Here we first show some code and add some different things to the code step by step to show you the functions and usage of scope types.
Our most original code is as follows:
static void Main(string[] args) { var myArray = new string[] { "Item1", "Item2", "Item3", "Item4", "Item5" }; for(int i=1; i <= 3; i++) { (myArray[i]); } (); }
Here we show the index 1-3 of our query array and output their values. There is no doubt that when we run the program, the code results are as follows:
Item2
Item3
Item4
However, suppose we don't want to use a for loop, but instead want to use this new feature called "range", we can rewrite the code to:
static void Main(string[] args) { var myArray = new string[] { "Item1", "Item2", "Item3", "Item4", "Item5" }; foreach (var item in myArray[1..3]) { (item); } (); }
Now let's run the program.
Item2
Item3
The result was one less than we expected. This is the first problem we encountered with scope types.
The starting index of the range is included, and the end index of the range is excluded.
If we modify our code:
static void Main(string[] args) { var myArray = new string[] { "Item1", "Item2", "Item3", "Item4", "Item5" }; foreach (var item in myArray[1..4]) { (item); } (); }
We will get the expected results.
Range abbreviation
It is very useful to use ranges to define the start and end index. But how do you represent the object from an index to the last array?
Starting from an index to the last object of the array
static void Main(string[] args) { var myArray = new string[] { "Item1", "Item2", "Item3", "Item4", "Item5" }; foreach (var item in myArray[1..]) { (item); } (); }
Output result:
Item2
Item3
Item4
Item5
From the first object of the array to the specified index
foreach (var item in myArray[..3]) { (item); }
Output result:
Item1
Item2
Item3
The entire array
foreach (var item in myArray[..]) { (item); }
Output result:
Item1
Item2
Item3
Item4
Item5
Start from an index in the array and all the way to an index at the end of the array
C# 8.0 provides the ^ operator, which means calculating the index from the end of the array.
foreach (var item in myArray[1..^1]) { (item); }
Output result:
Item2
Item3
Item4
Range Type
When we write 1..4, it looks like we are using the new syntax, which is actually just syntax sugar. In fact, it initializes a Range class object as if we can create an array using {"1", "2", "3"}.
static void Main(string[] args) { var myArray = new string[] { "Item1", "Item2", "Item3", "Item4", "Item5" }; Range range = 1..4; foreach (var item in myArray[range]) { (item); } (); }
Alternative Substring method
Another benefit of using range types is that you can use it to replace the method, which is easier to write.
("123456789"[1..4]);
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.