Improving our code readability with C# 9

Gal Ilinetsky
CodeX
Published in
3 min readOct 17, 2022

--

In C# 9 there are new language features that make our code more clean and readable. One of the major ones is Top-level statements.

Top-level statements:

The primary goal of the feature is to allow C# programs without unnecessary boilerplate around them, for the sake of learners and the clarity of code.

This is the default structure of the Main program:

If you look closely you will find, only one line of code which prints the string: “Hello World” to the console. We use twelve lines of code just to execute one.

Top-level statements enables us to get rid of this boilerplate code and just execute our code. The boilerplate code wrapping is still necessary, but it is autogenerated, and we don’t have to write it ourselves. Our code stays clear.

Main method of a Program class is the entry point of the program, therefore only one Main method can exist in our project. The same principle applies to top-level statements, there can be only one compilation unit (class) with top-level statements in our project.

In C# 9 we can create a simpler and clearer entry point for our application. It applies to both synchronous and asynchronous code.

Can we pass arguments? Yes, if you need to pass arguments to your application entry point you can access the string[] args directly as follow:

Target-typed new expressions

This feature allows us to avoid type specification for constructors when the type is known. The motivation for this feature is to allow field initialization without duplicating the type, which makes our code clean.

As we can see in the following example (line 7) it applies to more complicated constructors.

You might ask, what is this feature different from the option of using var data type? Apparently, it gives us the same solution (as you can see in line 5).

Var keyword only works for local variables. That means target-typed new expressions are especially powerful to initialize properties and fields(lines 9,22) because there the var keyword is not an option.

target-typed new expressions vs. var

Pattern-matching

Pattern matching is a technique where you test an expression to determine if it has certain characteristics. C# pattern matching provides more concise syntax for testing expressions and taking action when an expression matches.

Pattern matching existed in C# in earlier versions, one of them was the is pattern expression.

Relational patterns

In a relational pattern, you can use any of the relational operators <, >, <=, or >= to test how a value compares to constants. You can use them not only with is expressions but also in switch expressions.

Without relational patterns, we would have to use multiple if-else statements, which makes our code less elegant and more complex.

If you look carefully at the previous example you will see that we used another new feature :

Logical patterns

Beginning with C# 9.0, you use the not, and, and or pattern combinators

In the following example you can see the use of this pattern to check that product is not null and check if its price is in a certain range:

As you can see our code looks much more readable After C# 9 new features.

--

--

Gal Ilinetsky
CodeX
Writer for

Software Engineer, .net development focus. Here to share my knowledge on points of view on software development fields I take interest in.