Home > database >  What is double exclamation mark in C#?
What is double exclamation mark in C#?

Time:02-12

From https://source.dot.net/#System.Private.CoreLib/Hashtable.cs,475:

public virtual bool ContainsKey(object key!!)

It looks like two null-forgiving operators. Is there a document about it?

CodePudding user response:

This is a null-parameter check syntax being introduced in C# 11.

The proposal is here, and the PR doing a first roll-out to the runtime is here.

The syntax:

public void Foo(string bar!!)
{
}

Is roughly equivalent to:

public void Foo(string bar)
{
    if (bar is null)
    {
        throw new ArgumentNullException(nameof(bar));
    }
}

... although the actual implementation uses a throw helper, See on SharpLab.

  • Related