C# 8.0 Ranges and Indices Types | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 21, 2020
3 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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)}");
Advertisement

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.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.