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

Viewed 661

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?

2 Answers

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.

Related