The release of .NET 6 brings along some awesome new features to C#11. The pace at which Visual Studio, .NET and C# are moving is relentless. Developers are being bombarded with new features and the temptation to stay on the cutting edge is ever present. Today we are going to talk about some of the new features coming to C# currently in Preview with Visual Studio 2022 Update 1 and .NET 6 SDK 6.0.200.
One of the most useful things I was able to get my hands on is parameter null-checking. What would normally look something like this.
Can now be abbreviated to something much more compact.
Here is how it works
Code is automatically generated to check the null condition. The null check happens before any of the method's code is executed. There are some caveats to this new feature, for example this check can only be applied to parameters when there is an implementation, for instance this does not apply for scenarios with abstract method parameters.
Parameter null-checking on iterator methods
Another interesting case is explained by Kathleen Dollard, Principal Program Manager for .NET as shown below.
The syntax is also valid on parameters to iterator methods. The null-check will occur when the iterator method is invoked, not when the underlying enumerator is walked. This is true for traditional or async iterators:
- Kathleen Dollard, Principal Program Manager for .NET
There are a few more features included in this preview release. Things like, a very cool new way to declare strings, as well as a new List patterns feature. To read more about these and more things coming to C#, head over to the .NET blog and check out this awesome post by Kathleen Dollard.
How about getting involved? If you would like to be part of community shaping the future of C#, check out the CSharp repo over at GitHub and get involved!