What’s new in C# 12

As technology continues to evolve, so does the C# programming language. With the release of C# 12 on the horizon, developers can look forward to a host of exciting new features and enhancements that promise to streamline their coding experience and open up new possibilities in software development. If you haven’t yet switched to C#…

How to switch to C# 12

While trying out C# 12 features, it’s possible you might come across errors similar to these: Feature is not available in C# 11.0. Please use language version 12.0 or later. or Error CS8652: The feature ‘<feature name>’ is currently in Preview and unsupported. To use Preview features, use the ‘preview’ language version. There are two…

C# 12 – Default values for parameters in lambda expressions

Starting with C# version 12, you can specify default values for your parameters in lambda expressions. The syntax and the restrictions on the default parameter values are the same as for methods and local functions. Let’s take an example: This lambda can now be consumed as follows: params array in lambda expressions You can also…

C# 11 – Generic attributes

All right folks, generic attributes are finally a thing in C#! 🥳 You can define one just as you would define any other generic class: And use it like you would use any other attribute: Generic attribute restrictions When applying the attribute, all the generic type arguments must be provided. In other words, the generic…

C# 11 – Raw string literals

Raw string literals are a new format which enables you to include whitespace, new lines, embedded quotes, and other special characters in your string, without requiring escape sequences. How does it work: A quick example: This will output the following: Whitespace before the closing sequence The whitespace before the closing double-quotes controls the whitespace which…

How to switch to C# 11

Feature is not available in C# 10.0. Please use language version 11.0 or later. There are two ways to approach this: Language version is greyed out and cannot be modified The Language version cannot be changed from the Properties window of the project. The version is linked to the target .NET framework version of your…

C# – Best way to Throw Exception If Null

.NET 6 introduced a few new helper methods to deal with throwing exceptions, and one of them is ThrowIfNull. The usage is simple: The method will throw a ArgumentNullException when myParam is null. It will do nothing otherwise. ThrowIfNull can take in two parameters: Note: paramName uses CallerArgumentExpressionAttribute to retrieve the name of your parameter…