【问题标题】:Creating a custom operator - '!?' (negation of '??'). Is it possible? [duplicate]创建自定义运算符 - '!?' (否定'??')。可能吗? [复制]
【发布时间】:2014-08-21 17:45:51
【问题描述】:

是否可以创建像“!?”这样的自定义运算符('??'的否定)而不是写长表达式:

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

我想拥有:

var newValue = value !? 5;

谢谢!

【问题讨论】:

  • value !? 5value ?? 5 有何不同?因为,newValue = value != null ? 5 : (int?)null;newValue = value == null ? (int?)null : 5; 相同

标签: c# .net new-operator


【解决方案1】:

没有。

您不能在 C# 中创建自己的运算符。您只能覆盖该语言中已经存在的(一些)。

【讨论】:

    【解决方案2】:

    你不能在 C# 中定义 new 运算符 - 唯一的方法是重新定义现有的,但是你 can not redefine ?: and ?? operators

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      相关资源
      最近更新 更多