February 7

How to use Indexes in C# 8


Learn how to use Indexes in C# 8 and Visual Studio 2019.

Have you enjoyed this video?

Subscribe now to the Productive C# YouTube channel and our newsletter to be the first to receive new videos and articles on C# and .NET.

Transcript of the video

Hi, I am Andrea and welcome to Productive C#.

In this video I want to talk about Indexes in C# 8.

Here I have a Console application that create a string text and an array of numbers and try to print the last character of the string and the second to last number from the array. If I run this program, in fact, you can see that 9 is printed that is the last character and 40, that is the second to last number is printed. In order to do that is to simply access the property Lenght of the string and the array and then substract the index that we want to actually access. This allows us to get a final index on the array or the string to access the character or number we are looking for. This is how you would do this traditially with C#.

Starting from C# 8, now you can do this in a very easy way using the hat operator called the index operator using the hat symbol. You can do just ^1 or ^2 to access the last element or the second to last element. And this is pretty much exactly the same behavior. It compiles using the C# 8 compilers and it works. What happen under the cover is very important. The compiler translates this code into the instantiation of a Index struct that takes the number you provided and fromEnd boolean set to true. This is effectively what happen under the cover. As you can see, this Index struct allow you to represent an Index starting from the end of the collection or from the beginning if you put false here but in that case it make sense to use an integer directly instead of the Index struct. This is basically what you can do.

An another important thing is that if you want you can use a variable. You don't need to do this just using a constant but you can actually do index operator and pass a variable and achieve the same thing. here I can do ^n and here ^(n+1). 

This syntax is supported for strings, for arrays and for Span<T> but unfortunately is not working yet for List<T>. Let's convert numers into a List and if you wait a second you get a compiler error: you can't convert a System.Index to an int. The reason why this is not supported is because List now doesn't have an indexer that takes an Index as parameter defined. Microsoft didn't add this support yet on the collections but I hope Microsoft will do that.

Here I have a small example of a class that inherits from List and add this support just as an example to show how you can add support for indexes on your own types. It's very simple. You create an indexer that takes an Indexs as parameter, then you basically look at the FromEnd property, if it is true it means that you need to start from the end and here you need to do the same operation we were doing before Count - value otherwise I use simply the value. I use base to get access the the indexer defined in List. I do not recommend to inherit from List like that and this is just an idea on how you can add support for indexes in your own types if you need it. Hope Microsoft add this support soon in .NET Core 3.

 This is a very simple feature that gives you the ability to access elements from the end of a collection in very compact way. This is very handy. This feature comes together with Ranges that I will cover in a future video but basically it allows you to given two indexes extract a subcollection from a collection. It allows to basically create a slide of a collection in a very nice way. 

There is only one thing I want to mention that is very important but maybe not immediately obvious. Here I am using ^1 in order to access the last element instead of ^0. You use 0 if you want to get the first element of a collection but you use ^1 in order to access sthe last element. Just pay attention that the last element start from 1 instead of 0. The main reason why Microsoft did it is because when you use the indexes for ranges, the upper end index of a range is alwyas exclusive. This is it.

Thank you very much for watching.

Have you enjoyed this video?

Subscribe now to the Productive C# YouTube channel and our newsletter to be the first to receive new videos and articles on C# and .NET.

Check out these C#.NET posts:


You may also like

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Subscribe to our newsletter now!

Get instant access to the Master C# 9 webinar to learn all the new exciting C# 9 features quickly.
Get regular videos and news on C# and .NET and offers on products and services to master C#.

We will collect, use and protect your data in accordance with our Privacy Policy.