2

This is my very first question here. I bumped into a method like this in C#:

void Do(string action!) { ... }

And don't get what the ! after action is and it does. Can you help me understand it?

Majid Parvin
  • 3,689
  • 5
  • 27
  • 41

2 Answers2

6

This is named the bang operator, !, which can be positioned after any identifier in a parameter list and this will cause the C# compiler to emit standard null checking code for that parameter. For example:

void Do(string action!) { ... }

Will be translated into:

void Do(string action!) {
    if (action is null) {
        throw new ArgumentNullException(nameof(action));
    }
    ...
}

btw, at the moment, this feature is not available yet. You can find more info here.

Majid Parvin
  • 3,689
  • 5
  • 27
  • 41
0

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving

you use the null-forgiving operator to declare that expression x of a reference type isn't null