Champions of C# 9 – Part 1: „Simplified parameter null validation code“


Published: 03.01.2020

In this series of blog posts I wan’t to have a closer look at the current proposals for the upcoming C# Version 9. This will be based on their current status as „Champion“, which make them very likely to be adopted for the final release.

In this part I will write about Proposal #2145 – Simplified parameter null validation code.

Usually, if handing over parameters to a method, some checking for NULL is required to prevent unexpected behavior. Have a look at the following sample of a method with some checking.

In C# 9, this code could be simplified by adding ! as an indicator for „Will not be NULL“ to the parametername. The code would then look like this.

Based on This discussion from summer 2019, the new syntax is supposed to throw an Exception too, if the constraint is violated. Most likely also an ArgumentException, but this doesn’t seem to have specified yet.


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.