The Null Method Operator "?!"

time to read 2 min | 354 words

After reading this, I was reminded how much I want the null method operator. Here is the scenario:

string name = user != null ? user.Name : "no-name";

The Null Method Operator work like this:

string name = user.Name ?! "no-name";

The rules:

  • The first object reference is checked, and if it is null, it skips the method call and goes to the other line.
  • Only the first object reference is checked. Chained method calls are not supported, this should throw an NullReferenceError, as usual.
    string name = user.ReturnNull.ToString() ?! "no-name";
  • You can chain the calls, like this:
    string name = user.Name ?! Context.User.Identity.Name ?! "really-no-name";

I really can't express how much I would like this feature. And have the null coalesing operator is like a thorn in my foot, everytime I think about it, I get annoyed that I don't hve the Null Method Operator.

Feel free to come with a better name.