Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the ! mean in 'return default!;' in C# logging implementation? [duplicate]

Tags:

c#

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!;
like image 628
CausingUnderflowsEverywhere Avatar asked Feb 21 '26 03:02

CausingUnderflowsEverywhere


1 Answers

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.

like image 123
CausingUnderflowsEverywhere Avatar answered Feb 22 '26 15:02

CausingUnderflowsEverywhere



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!