Alex Gobiat
Alex Gobiat

Reputation: 101

C# what is the exclamation point positioned after the parameter name

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?

Upvotes: 9

Views: 3484

Answers (2)

Majid Parvin
Majid Parvin

Reputation: 5002

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.

Upvotes: 11

Anton Christiansen
Anton Christiansen

Reputation: 316

https://learn.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

Upvotes: 3

Related Questions