C# 8.0 Expected Features Part – IV : Something new for Indexes and ranges

C# 8.0 Features

In my previous post, I have explained about one of the features of C# 8.0 which is Switch expression.

Have a look here for other features of C# 8.0.

In this post, I will explain one more future feature which is called Ranges.

This feature creates 2 new objects, one is System.Index and another is System.Range and we can use them to index/slice collections at run time.

Let us see what it is.

System.Index

There would be a very few big applications which does not deal with the collections and when there is a collection, there are the indexes.

Let us take an example where we have a collection and we want to take the last character of the collection, in such case there is not any straight forward syntax in C#.

So we mainly use it as below:


var last = myCollection[myCollection.Length-1];

This syntax may lead the application into the errors.

C# 8.0 will introduce one new operator which is called the Hat operator ^.

So the same syntax, we wrote above, can be written as below:


var last = myCollection[^1];  // myCollection[Index.CreateFromEnd]

In short, the Index expression means from the end.

We can create multidimensional list as below:


var multiDimensional = list[3, ^2] // list[3, Index.CreateFromEnd(2)]

System.Range

System.range will make all range related operation in collection easier.

If you know Python, then you may compare this feature with the Slice operator feature of Python.

C# 8 will introduce a new range operator x..y. It is a binary infix operator that accepts two expressions.

For example you have a list and you want to bypass or take some elements in sequence, you have to use .Skip() or .Take() of LINQ which gets little uglier for complex scenarios.

Take a look at this SO thread, currently we do not have any straight forward syntax for these slicing of the collection. We have to use LINQ for that.

Let us say, you want to take first 5 characters from a string, currently you would write the code as below:


var firstFive = yourStringVariable.Substring(0,5);

With the new range expression, it could be written as below:


var  firstFive = yourStringVariable[0..5];

One more example where you want to skip 2 characters and then take 2 characters, we have to use skip and take as below:


var result1 = myList.Skip(2).Take(2);

The same operation would be easily written with Range expression as below:


var result = myList[2..4];

There would be plenty of usage of this range expression, one of them would be in the switch cases:


switch (number)

{

case 1..10: do the same for all of them;

case 11..20: again, same thing for these numbers;

}

Let us combine Index expression with range expression and let us take some more examples.

We have a list of characters as below:


list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

Now we want some different results like:

  • Skip 2 characters and take 2 characters : list[2..4];
  • Skip first and last character: list[1..^1];
  • Last 3 characters: list[^3];
  • First 4 characters: list[0..4];
  • Range start from 2: list[2..];
  • Skip last 3 characters: list[..^3];
  • Range all: list[..];

Hope it helps.

9 thoughts on “C# 8.0 Expected Features Part – IV : Something new for Indexes and ranges

Leave a comment