dcsimg

C# 8.0 Ranges and Indices Types

By Chandra Kudumula

Introduction

We will explore a couple of new features, which are introduced in C# 8.0—Ranges and Indices.

Range Operator

In C# 7.0, we have a span data type. Span is useful; it allows us to access part of a structure in a contiguous sequence of another structure such as an array or a string.

Let's see an example of the Span type.

   var arrNumbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   var slice = arrNumbers.AsSpan().Slice(3, 2);

Now, the slice has 4 and 5.

With C# 8.0, we can extract part of a structure not only from span data type but also from arrays directly by using a range operator. Range operator syntax is easy to use and is represented by ..(two dots).

Following is an example for range elements.

   var arrNumbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   var rangeNumbers = arrNumbers[3..6];
   Console.WriteLine($"{string.Join(", ", rangeNumbers)}");

This code will print 4, 5, 6 to the console.

Note: It starts with index 3(element 4) and ends with index 5(Element 6), because in a range the beginning index is INCLUSIVE and the end index is EXCLUSIVE.

We can create a range and use it like any other variable. Following is the sample code for the range variable.

   var arrNumbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   Range range = 3..5;
   var rangeNumbers = arrNumbers[range];
   Console.WriteLine($"{string.Join(", ", rangeNumbers)}");

This code will print 4, 5 to the console.

Next is the sample code to print all numbers in an array using range.

   var arrNumbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   var allNumbers = arrNumbers[..];
   Console.WriteLine($"{string.Join(", ", allNumbers)}");

This code will print all numbers in an array.

Note: [..] is equal to [0..10] if an array length is 10.

Following is the sample code to print all the items except the first in an array.

   var arrNumbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   var allButNotFirst = arrNumbers[1..];
   Console.WriteLine($"{string.Join(", ", allButNotFirst)}");

Index Operator

Index is a reference to a location in a sequence. The index is represented by the ^(hat) operator. The main advantage of an Index operator is to declare an index relative to the end of a sequence.

As we know, in an array, Index 0 represents the first element and array length and – 1 gives the last element of an array. Instead of using array length - 1 to get the last element in an array, we will use the ^(hat) operator.

Next is the sample code to get the last element from an array by using the ^(hat) operator.

   var arrNumbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   var lastItem = arrNumbers[^1];
   Console.WriteLine(lastItem);

This snippet will print 10 to the console.

Note: We are using ^1 to get the last element in an array but not ^0. This is because C# uses a 0-based index.
  • ^0 is equal to array length (10 – 0 = 10 Throws index out of range exception)
  • ^1 is equal to array length – 1 (10 – 1 = 9 index of last element)
  • ^2 is equal to array length – 2 (10 – 2 = 8 index of the second item from the end) and so on.

We can assign an index to a variable.

Following is the sample code to get second element from the last in an array using index variable.

   var arrNumbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   Index idx = ^2;
   var idxNumber = arrNumbers[idx];
   Console.WriteLine(idxNumber);

This will print 9 to the console.

We also can use Ranges and Indices together.

Next is the sample code to print the last four elements from an array by using range and index.

   var arrNumbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   var lastFourItems = arrNumbers[^4..];
   Console.WriteLine($"{string.Join(", ", lastFourItems)}");

This code will print 7, 8, 9, 10 to the console.

Following is the sample code to create a range from variables.

   var arrNumbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   var location = 4;
   Index startIndex = ^location;
   Range range = startIndex..;
   var lastFourItems = arrNumbers[range];
   Console.WriteLine($"{string.Join(", ", lastFourItems)}");

This will print 7, 8, 9, 10 to the console.

Conclusion

Ranges and Indices are useful additions to the C# language and framework. Ranges can be used on arrays and span types to extract the subset of a sequence by using the ..(two dots) operator. An index can be used to define an index as a sequence by using the ^(hat) operator.



This article was originally published on February 21st, 2020

Related Articles

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date