可以重写null-coalescing运算符吗?

时间:2022-04-23 11:47:21

Is it possible to override the null-coalescing operator for a class in C#?

是否可以重写c#中的类的null-coalescing操作符?

Say for example I want to return a default value if an instance is null and return the instance if it's not. The code would look like something like this:

比方说,如果实例为空,我想返回一个默认值,如果没有返回实例,则返回实例。代码是这样的:

   return instance ?? new MyClass("Default");  

But what if I would like to use the null-coalescing operator to also check if the MyClass.MyValue is set?

但是,如果我想使用null-coalescing运算符来检查MyClass是否也是这样。MyValue设置?

Of course there is no real need for this (at least I think so) - so before you answer "why would you want to do that" - I am just curious if it's possible.

当然没有必要这样做(至少我是这么认为的)——所以在你回答“你为什么要那样做”之前——我只是好奇这是否可能。

5 个解决方案

#1


29  

Good question! It's not listed one way or another in the list of overloadable and non-overloadable operators and nothing's mentioned on the operator's page.

好问题!在可重载和不可超载的操作符列表中,它并没有列出任何一个方法,在操作符的页面上也没有提到。

So I tried the following:

所以我尝试了以下方法:

public class TestClass
{
    public static TestClass operator ??(TestClass  test1, TestClass test2)
    {
        return test1;
    }
}

and I get the error "Overloadable binary operator expected". So I'd say the answer is, as of .NET 3.5, a no.

我得到的错误是“预期的可重载二进制运算符”。所以我的答案是。net 3.5是否定的。

#2


14  

According to the ECMA-334 standard, it is not possible to overload the ?? operator.

根据ECMA-334标准,不可能使?操作符。

Similarly, you cannot overload the following operators:

同样,您不能重载以下操作符:

  • =
  • =
  • &&
  • & &
  • ||
  • | |
  • ?:
  • 吗?
  • checked
  • 检查
  • unchecked
  • 无节制的
  • new
  • typeof
  • typeof
  • as
  • 作为
  • is

#3


6  

Simple answer: No

简单的答案:没有

C# design principles do not allow operator overloading that change semantics of the language. Therefore complex operators such as compound assignment, ternary operator and ... can not be overloaded.

c#设计原则不允许操作符重载改变语言的语义。因此,复杂算子如复配、三元算子和…不能重载。

#4


2  

This is rumored to be part of the next version of C#. From http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated

据说这是c#的下一个版本的一部分。从http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated

7. Monadic null checking

7所示。一元null检查

Removes the need to check for nulls before accessing properties or methods. Known as the Safe Navigation Operator in Groovy).

删除在访问属性或方法之前检查null的需要。在Groovy中称为安全导航操作符)。

Before

之前

if (points != null) {
    var next = points.FirstOrDefault();
    if (next != null && next.X != null) return next.X;
}   
return -1;

After

var bestValue = points?.FirstOrDefault()?.X ?? -1;

#5


-2  

If anyone is here looking for a solution, the closest example would be to do this

如果有人在这里寻找解决方案,最贴切的例子就是这样做

return instance.MyValue != null ? instance : new MyClass("Default");

#1


29  

Good question! It's not listed one way or another in the list of overloadable and non-overloadable operators and nothing's mentioned on the operator's page.

好问题!在可重载和不可超载的操作符列表中,它并没有列出任何一个方法,在操作符的页面上也没有提到。

So I tried the following:

所以我尝试了以下方法:

public class TestClass
{
    public static TestClass operator ??(TestClass  test1, TestClass test2)
    {
        return test1;
    }
}

and I get the error "Overloadable binary operator expected". So I'd say the answer is, as of .NET 3.5, a no.

我得到的错误是“预期的可重载二进制运算符”。所以我的答案是。net 3.5是否定的。

#2


14  

According to the ECMA-334 standard, it is not possible to overload the ?? operator.

根据ECMA-334标准,不可能使?操作符。

Similarly, you cannot overload the following operators:

同样,您不能重载以下操作符:

  • =
  • =
  • &&
  • & &
  • ||
  • | |
  • ?:
  • 吗?
  • checked
  • 检查
  • unchecked
  • 无节制的
  • new
  • typeof
  • typeof
  • as
  • 作为
  • is

#3


6  

Simple answer: No

简单的答案:没有

C# design principles do not allow operator overloading that change semantics of the language. Therefore complex operators such as compound assignment, ternary operator and ... can not be overloaded.

c#设计原则不允许操作符重载改变语言的语义。因此,复杂算子如复配、三元算子和…不能重载。

#4


2  

This is rumored to be part of the next version of C#. From http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated

据说这是c#的下一个版本的一部分。从http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated

7. Monadic null checking

7所示。一元null检查

Removes the need to check for nulls before accessing properties or methods. Known as the Safe Navigation Operator in Groovy).

删除在访问属性或方法之前检查null的需要。在Groovy中称为安全导航操作符)。

Before

之前

if (points != null) {
    var next = points.FirstOrDefault();
    if (next != null && next.X != null) return next.X;
}   
return -1;

After

var bestValue = points?.FirstOrDefault()?.X ?? -1;

#5


-2  

If anyone is here looking for a solution, the closest example would be to do this

如果有人在这里寻找解决方案,最贴切的例子就是这样做

return instance.MyValue != null ? instance : new MyClass("Default");