-1

Is it possible to create a custom operator like '!?' (negation of '??') instead of writing long expression:

int? value = 1;    
var newValue = value != null ? 5 : (int?)null;

I want to have:

var newValue = value !? 5;

Thanks!

  • How is `value !? 5` any different from `value ?? 5`? Because, `newValue = value != null ? 5 : (int?)null;` is the same as `newValue = value == null ? (int?)null : 5;` – AustinWBryan May 18 '18 at 00:57

2 Answers2

3

No.

You cannot create your own operators in C#. You can only override (some of the) ones that already exist in the language.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
2

You cannot define new operator in C# - the only way is to redefine existing ones, but you can not redefine ?: and ?? operators.

Spook
  • 25,318
  • 18
  • 90
  • 167