Pro EP 19 : Using Range for Slicing Array in C# 8.0

Muhammad Waseem
Become .NET Pro !
2 min readMar 4, 2023

In previous episode we talked about Indices , Indices and Range were introduced together in C# 8.0 . Both comes from 𝚂𝚢𝚜𝚝𝚎𝚖.𝙸𝚗𝚍𝚎𝚡 and 𝚂𝚢𝚜𝚝𝚎𝚖.𝚁𝚊𝚗𝚐𝚎. Range simplifies our code by using two dots followed by starting and ending index.

Range syntax is cool and looks more natural, it looks like this 𝚂𝚝𝚊𝚛𝚝..𝙴𝚗𝚍 , start is the starting index and it is always inclusive and end is the ending index which would be exclusive.

Using range we can extract a substring from a larger string without having to use the Substring method. Range can lead to cleaner and more concise code, making it easier to read and maintain and avoiding use of LINQ.

Edit : Last example in 2.0 will print Bilal, Waleed.

We can achieve this using Range as well.

If you want to help the author in growing

  1. Subscribe my YouTube Channel where I would be sharing videos on .NET
  2. Subscribe my Weekly .NET Newsletter of C#/.NET
  3. Download my eBook at Gum road that contains 30+ .NET Tips.

--

--