In the create a custom logger example the function IDisposable BeginScope<TState>(TState state); of the ILogger interface is implemented as return default!; with no explanation of what BeginScope even is and why default! is used.
I am specifically interested why the null forgiving operator ! was used here.
public IDisposable BeginScope<TState>(TState state) => default!;
The ! operator is the null-forgiving-operator introduced in C# 8.0. It marks things as never being null and instructs the compiler to skip null checks so you don't get a "can be null" warning. It's useful when you know a variable will never be null at a spot in your code, especially after you've used the null-able operator ? to designate a variable as null-able.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With