Detailed explanation of range type example in C × 8.0

Time:2020-2-19

Preface

C ා language was released in 2000. Up to now, 7 versions have been officially released. Each version contains many exciting new features and functional updates. At the same time, the release of each version of C# is highly coupled with the Visual Studio and.NET runtime version in the same period, which is also helpful for developers to better learn and master C#, and combine it with the use of Visual Studio and.NET.

A new 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 function and usage of scope type.

Our 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++)
 {
  Console.WriteLine(myArray[i]);
 }
 
 Console.ReadLine();
}

Here we show the index 1-3 that defines our query array and outputs their values. There is no doubt that when we run the program, the code results are as follows:

Item2
Item3
Item4

However, if we don’t want to use the for loop, but want to use this new feature called “range”, we can rewrite the code as follows:


static void Main(string[] args)
{
 var myArray = new string[]
 {
  "Item1",
  "Item2",
  "Item3",
  "Item4",
  "Item5"
 };
 
 foreach (var item in myArray[1..3])
 {
  Console.WriteLine(item);
 }
 
 Console.ReadLine();
}

Now let’s run the program.

Item2
Item3

The result was one less than we expected. This is the first problem we have with scope types.

The start index of the range is included, and the end index of the range is excluded

If we change our code:


static void Main(string[] args)
{
 var myArray = new string[]
 {
  "Item1",
  "Item2",
  "Item3",
  "Item4",
  "Item5"
 };
 
 foreach (var item in myArray[1..4])
 {
  Console.WriteLine(item);
 }
 
 Console.ReadLine();
}

We will get the expected result.

Scope abbreviation

Using ranges to define start and end indexes is very handy. But how do you represent an object that starts with an index and ends with an array?

From the beginning of an index to the last object of an array


static void Main(string[] args)
{
 var myArray = new string[]
 {
  "Item1",
  "Item2",
  "Item3",
  "Item4",
  "Item5"
 };
 
 foreach (var item in myArray[1..])
 {
  Console.WriteLine(item);
 }
 
 Console.ReadLine();
 
}

Output results:

Item2
Item3
Item4
Item5

From the first object of the array to the specified index


foreach (var item in myArray[..3])
{
 Console.WriteLine(item);
}

Output results:

Item1
Item2
Item3

Entire array


foreach (var item in myArray[..])
{
 Console.WriteLine(item);
}

Output results:

Item1
Item2
Item3
Item4
Item5

From an index of the array to an index at the end of the array

C ා 8.0 provides the ^ operator, which means the index is calculated from the end of the array.


foreach (var item in myArray[1..^1])
{
 Console.WriteLine(item);
}

Output results:

Item2
Item3
Item4

Scope type

When we write 1.. 4, it looks like we are using a new syntax. Actually, it is just a syntax sugar. In fact, it initializes a range class object, just like 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])
 {
  Console.WriteLine(item);
 }
 
 Console.ReadLine();
}

Replace substring method

Another advantage of using scope type is that you can use it to replace the string. Substring method, which is easier to write.


Console.WriteLine("123456789"[1..4]);

summary

The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.