CODEX

5 Ways To Drastically Shorten Your Code in C# 8.0

Save time and increase your development with these 5 techniques

Arnold Abraham
CodeX
Published in
5 min readMar 22, 2021

--

What’s New In C# 8 (All Rights Reserved by CodingTech)

The C#8.0 included extra features, officially released by Microsoft, which can streamline code. To use them correctly, I will demonstrate five methods to shorten your code drastically and compare them with the previous language constructs. Microsoft provides a detailed overview. [1]

The new version of C# offers various new features, allowing us to use some instructions for code that are more elegant than before. Pattern matching, for example, which has been further developed here, plays a major role.

1. Indexes

We begin by introducing the new functions for indexed access. The following example exhibits the indexed access to a string and an array with integer values, as it was usual until now:

The outputs are now the character M and the digit 2. C#8.0 offers an index operator that counts from the end of a string and is represented by the character ‘^’. With this operator, we can also adjust the code as follows:

The execution of the code leads to the same outputs. The ‘^’ character is a shortened notation for the also new data type index. The code in Figure 1 is functionally identical to the one adapted to C#8.0, the constructor is supplied with the index position, and I added the specification of whether the index counted from the front (true) or from the back.

Using variables is, of course also possible, as seen here:

--

--

Arnold Abraham
CodeX
Writer for

JavaScript, TypeScript and C#/.NET Tutorials/News/Best Practices by a German Software Engineer - Fun helps you to learn on the fly --> arnoldcode.com