ICYMI C# 8 New Features: Simplify Array Access and Range Code

 ICYMI C# 8 New Features: Simplify Array Access and Range Code

This is part 5 in a series of articles.One of the new features that C# 8 introduced was the ability to work more simply with arrays and items within arrays.One of the new features that C# 8 introduced was the ability

This is part 5 in a series of articles.

One of the new features that C## 8 introduced was the ability to work more simply with arrays and items within arrays.

Take the following code that uses various ways to manipulate an array of strings:

?

It’s pretty easy to get the first element in an array [0] and the last item [letters.Length - 1] but when we get to getting ranges (like the middle 2 letters) or 2 from the end things get a little more complicated.

C## 8 introduced a new shorter syntax for dealing with indices and ranges. The preceding code could be re-rewritten in C## 8 as follows:

There’s a few different things going on in the C#8 version.

First of all, C## 8 gives us the new index from end operator ^. This essentially gives us an element by starting at the end and counting back. One thing to note here is that the index ^0 is not the last element, rather the length of the array (remember in C## arrays are zero-based). The last element is actually at ^1. If you try and access an element using [^0] you’ll get an IndexOutOfRangeException just as you would if you wrote [letters.length].

In additional to the index from end operator, C## 8 also introduced the range operator .. – *this allows you to specify a range of elements. For example in the code, the range *[2..4] gives us the middle 2 letters C & D – or more specifically, it gives us the range of elements starting at 2 and ending at 4. Why 4 though? When using the range operator, the first index is inclusive _but the last index is _exclusive. So [2..4] really means “2 to 3 inclusive”.

You can also use open ended ranges where you omit the start or end range, for example string[] lastThreeLetters = letters[^3..];

To make the new indexing feature work, there is a new struct called Index. And to make ranges work there is a new struct called Range.

For example, you can create and pass Range instances around:

Notice in this code we are using ^0 (and get no exception) to specify the end because the end of a range is exclusive.

As well as arrays you can also use these techniques with other types which you can read more about in the Microsoft documentation e.g. the MS docs state: “For example, the following .NET types support both indices and ranges: StringSpan, and ReadOnlySpan. The List supports indices but doesn't support ranges.” And also from the docs: “Any type that provides an indexer with an Index or Range parameter explicitly supports indices or ranges respectively. An indexer that takes a single Range parameter may return a different sequence type, such as System.Span.”…and… “A type is countable if it has a property named Length or Count with an accessible getter and a return type of int. A countable type that doesn't explicitly support indices or ranges may provide an implicit support for them. For more information, see the Implicit Index support and Implicit Range support sections of the feature proposal note. Ranges using implicit range support return the same sequence type as the source sequence.”

icymi c# 8 c# 8

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Let's Give Some Unit Testing Love to C# 8 and C# 9 Features

How to use C#9 and C#8 features in JustMock unit tests

ICYMI C# 8 New Features: Write Less Code with Using Declarations

One nice little enhancement introduced in C# 8 helps to simplify code that uses disposable objects.

ICYMI C# 8 New Features: Simplify If Statements with Property Pattern Matching

This is part 3 in a series of articles. In the first part of this series we looked at switch expressions. When making use of switch expressions, C# 8 also introduced the concept of property pattern matching. This enables you to match on one or more items of an object and helps to simplify multiple if..else if statements into a more concise form.

ICYMI C# 8 New Features: Nested Switch Expressions

In this series we’ve already covered switch expressions and one little-known feature is the ability to nest switch expressions.

Dicey Issues in C/C++

C/C++ problems. If you are familiar with C/C++then you must have come across some unusual things and if you haven’t, then you are about to. The below codes are checked twice before adding, so feel free to share this article with your friends.