可以重载空合并运算符吗?
是否可以在 C# 中重载类的空合并运算符?
举例来说,如果实例为空,我想返回默认值,如果不是,则返回该实例。 代码看起来像这样:
return instance ?? new MyClass("Default");
但是,如果我想使用空合并运算符来检查 MyClass.MyValue 是否已设置,该怎么办?
Is it possible to overload the null-coalescing operator for a class in C#?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
据传这是 C# 下一版本的一部分。 来自http://damieng.com/blog/ 2013/12/09/probable-c-6-0-features-illustrator
7。 Monadic null 检查
在访问属性或方法之前无需检查 null 值。 在 Groovy 中称为安全导航操作符)。
之前
之后
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
7. Monadic null checking
Removes the need to check for nulls before accessing properties or methods. Known as the Safe Navigation Operator in Groovy).
Before
After
我试图用我编写的一个非常相似的
Nullable
结构来完成此任务。 使用Nullable
您可以执行类似的操作,将
id1
从Nullable
隐式转换为Guid< 没有问题/code> 尽管事实上
Nullable
仅 定义到类型T
的显式
转换。 用我自己的类型做同样的事情,它给出了一个错误所以我认为编译器中内置了一些魔法,可以为
Nullable
提供特殊的例外。 因此,作为替代方案...tl;dr
我们无法覆盖
??
运算符,但如果您希望合并运算符评估基础值而不是类(或在我的情况下是结构体)就其本身而言,您可以只使用一种方法,从而不需要额外的击键。 在我上面的例子中,它看起来像这样:用法:
这很有效,因为它是一个结构体,并且
id1
本身实际上不能为 null。 对于类,只要您尝试检查的值公开,扩展方法就可以处理实例是否为 null:用法:
I was trying to accomplish this with a struct I wrote that was very similar
Nullable<T>
. WithNullable<T>
you can do something likeIt has no problem implicitly converting
id1
fromNullable<Guid>
to aGuid
despite the fact thatNullable<T>
only defines anexplicit
conversion to typeT
. Doing the same thing with my own type, it gives an errorSo I think there's some magic built into the compiler to make a special exception for
Nullable<T>
. So as an alternative...tl;dr
We can't override the
??
operator, but if you want the coalesce operator to evaluate an underlying value rather than the class (or struct in my case) itself, you could just use a method resulting in very few extra keystrokes required. With my case above it looks something like this:Usage:
This works well since it's a struct and
id1
itself can't actually be null. For a class, an extension method could handle if the instance is null as long as the value you're trying to check is exposed:Usage:
如果有人在这里寻找解决方案,最接近的例子就是这样做
If anyone is here looking for a solution, the closest example would be to do this
好问题! 它没有以某种方式列在可重载和不可重载运算符列表 并且运营商页面上没有提及任何内容。
所以我尝试了以下操作:
并且收到错误“预期可重载二元运算符”。 所以我想说,从 .NET 3.5 开始,答案是否定的。
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:
and I get the error "Overloadable binary operator expected". So I'd say the answer is, as of .NET 3.5, a no.
根据 ECMA-334 标准,这是不可能的超载 ?? 操作员。
同样,您不能重载以下运算符:
According to the ECMA-334 standard, it is not possible to overload the ?? operator.
Similarly, you cannot overload the following operators:
简单的答案:没有
C# 设计原则不允许操作符重载改变语言的语义。 因此,复合赋值、三元运算符和...等复杂运算符不能重载。
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.