Playing around with C# indices and ranges

Let’s discover some cool C# features for working with sequences, indices and subranges!

This article is available on Medium.

No matter which type of software you’re working on, be it a web app, a video game or a bank internal tool, your project will always require data at some point. And, over the years, computer scientists have found lots of handy ways to represent data on computers. In particular, all modern programming languages like C# have nice containers and iterators to easily store and use “chunks” of related data (arrays, lists, etc.).

But sometimes, you don’t need to work on the full dataset all at once. You’re just interested in a sub-slice of it, and you need to quickly “cut and extract” this smaller block.

Luckily, C# has some really cool tips and tricks in store for us to quickly manipulate indices or ranges in arrays. So, today, let’s discover some of those 🙂

Example 1: Indexing an array… from the end?

Suppose you have some long web URL that you want to parse, and more precisely you just want to extract the very last “word”, i.e. the part that is after the last slash (here, the id bit):

A very basic way to do this is to first split the URL with the / character, and then access this temporary array at the “n – 1” cell:

But did you know you can actually do it with a one-liner? 😉

C# allows us to access array indices starting from the end by using the ^ character just before the index; so, for example, ^1 is the index to access the last element in our array:

Basically, ^n is equivalent to arr.Length - n. Of course, you still have to remember that arrays (and other int-indexed C# containers) are 0-indexed, so trying to access arr[^0] throws an exception, just like arr[arr.Length] does…

Example 2: Extracting a slice from an array

When you’re just reading some values for debug or display, it’s pretty easy to get whichever sub-range we want and just do the print for these values:

But something that is a bit more complex is when you want to get this sub-range and then apply various post-processing functions on it: sorting, filtering… You can obviously still write it inside a loop like before, but it can quickly become heavy for the readers, or even unoptimised if you have really large amount of data.

What if, instead, we could easily extract the range we want and store it in a variable?

This is something that is quite straight-forward when you use Lists, because they have a Range() method. But, for arrays, most naive approaches revolve around the following:

In other words, you just re-create a second array of the right size and fill it with the right elements in a loop. It works, but it’s not that pretty, nor that readable.

Instead, we can use yet another nice tool from C#, which is the .. operator:

Note: be careful – the .. operator excludes the stop index from the result (here, we don’t get the “h” at index 7, we stop just before).

Of course, you can mix the .. operator with the ^ one with saw before to get a slice with some “backward” index:

Example 3: Working in 2D

You might be already be familiar with multidimensional arrays, like this one:

But sadly, for those n-D arrays, ranges and slices aren’t available in C#? But you can find a workaround if, instead, you create an array of arrays, and then create ranges over the rows and columns accordingly! 🙂

For example, this example will output the middle-left numbers in the data table, i.e. 10, 11, 20 and 21:


Today, we’ve seen a few tricks for better slicing and indexing our data containers. Of course, this article was a short overview of the tool and there is a lot more to say and see – for more info, have a look at the Microsoft C# docs!

I hope you liked this short tutorial – and feel free to tell me in the comments what other C# topics you’d like to see me write about 🙂

Leave a Reply

Your email address will not be published.