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…

| |

How to publicly expose your local SignalR service for consumption by mobile clients using ngrok

When dealing with mobile clients, it’s not always easy to get them on the same network as your development machine and even when you do, localhost will have a different meaning so you need to use IPs and change bindings and disable SSL or trust self-signed certificates, in short it’s a pain. Say hello to…