Getting Setup With C# 8

If you aren’t sure if you are using C# 8, or you know you aren’t and want to know how to access these features. Read this quick guide on getting setup with .NET Core and C# 8.

Introduction To Ranges

Let’s first just show some code and from there, we can iterate on it trying a few different things.

Our starting code looks like :

static void Main(string[] args) { var myArray = new string[] { "Item1", "Item2", "Item3", "Item4", "Item5" }; for(int i=1; i <= 3; i++) { Console.WriteLine(myArray[i]); } Console.ReadLine(); }

So essentially we are looking to go from “index” 1 – 3 in our list and output the item. Unsurprisingly when we run this code we get

Item2 Item3 Item4

But let’s say we don’t want to use a for loop, and instead we want to use this new fandangle thing called “ranges”. We could re-write our code like :

static void Main(string[] args) { var myArray = new string[] { "Item1", "Item2", "Item3", "Item4", "Item5" }; foreach (var item in myArray[1..3]) { Console.WriteLine(item); } Console.ReadLine(); }

And when we run it?

Item2 Item3

Uh oh… We have one less item than we thought. We’ve ran into our first gotcha when using Ranges. The start of a range is inclusive, but the end of a range is exclusive.

If we change our code to :

static void Main(string[] args) { var myArray = new string[] { "Item1", "Item2", "Item3", "Item4", "Item5" }; foreach (var item in myArray[1..4]) { Console.WriteLine(item); } Console.ReadLine(); }

Then we get the expected result.

Shortcut Ranges

Using Ranges where it’s “From this index to this index” are pretty handy. But what about “From this index to the end”. Well Ranges have you covered!

From An Index (Inclusive) To The End (Inclusive)

static void Main(string[] args) { var myArray = new string[] { "Item1", "Item2", "Item3", "Item4", "Item5" }; foreach (var item in myArray[1..]) { Console.WriteLine(item); } Console.ReadLine(); }

Output :

Item2 Item3 Item4 Item5

From The Start (Inclusive) To An Index (Exclusive)

foreach (var item in myArray[..3]) { Console.WriteLine(item); }

Output :

Item1 Item2 Item3

Entire Range (Inclusive)

foreach (var item in myArray[..]) { Console.WriteLine(item); }

Output :

Item1 Item2 Item3 Item4 Item5

From Index (Inclusive) To X From The End (Inclusive)

This one deserves a quick description. Essentially we have a new operator ^ that is now used to designate “From the end”. I labelled this one as “Inclusive” but it really depends on how you think about “from the end”. In my mind in a list of 5 items ^1 would refer to the 4th item. So if that’s included in the result, it’s inclusive.

foreach (var item in myArray[1..^1]) { Console.WriteLine(item); }

Output :

Item2 Item3 Item4

Range As A Type

When we write 1..4, it looks like we are dealing a couple of integers in a new special format, but actually this is almost the initialisation syntax for a range. Similar how we might use {“1”, “2”, “3”} to illustrate us creating an array or list.

We can actually rip out the range from here, and create a new type to be passed around.

static void Main(string[] args) { var myArray = new string[] { "Item1", "Item2", "Item3", "Item4", "Item5" }; Range range = 1..4; foreach (var item in myArray[range]) { Console.WriteLine(item); } Console.ReadLine(); }

This could be handy to pass around into a method or re-use multiple times. I think it’s pretty close to using something like the System.Drawing.Point object to specify X and Y values rather than having two separate values passed around.

A New Substring

One pretty handy thing about ranges is that they can be used as a faster way to doString.Substring operations. For example :

Console.WriteLine("123456789"[1..4]);

Would output 234 . Obviously this would be a slight shift in thinking because string.Substring isn’t “From this index to this Index” but instead “From this index count this many characters”.

Who Uses Arrays Anyway? What About List<T>?

So when I first had a play with this. I couldn’t work out what I was doing wrong… As it turns out. It was because I was using a List<T> instead of an Array. I don’t to be too presumptuous but I would say the majority of times you see a “list” or “set” or data in a business application, it’s going to be of type List<T> . And well.. It’s not available to use Ranges. I also can’t see any talk of it being made either ( Possibly because of the linked list nature of List<T> Apparently List<T> is not a linkedlist at all! So then I’m not sure why Ranges are not available). Drop a comment below on your thoughts!