什么是提升运营商? [英] What are lifted operators?

查看:168
本文介绍了什么是提升运营商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这篇文章和我挣扎遵循VB.NET的例子,说明解除运营商。似乎没有成为一个等价的C#示例或教程。我没有与操作符重载一般,所以苦思与VB.NET当量计算,同时对可空类型读了太多的经验可能不是开始...最好的地方

I was looking at this article and am struggling to follow the VB.NET example that explains lifted operators. There doesn't seem to be an equivalent C# example or tutorial. I don't have much experience with operator overloading in general, so trying to come to terms with the VB.NET equivalent whilst reading up on nullable types probably isn't the best place to start...

会有人能够提供提升运营商的解释,他们是如何使用可空类型?难道它只是意味着可空类型本身不超载运营,并将使用运营商的基本类型,它重新presents?

Would anyone be able to provide an explanation of lifted operators and how they are used by nullable types? Does it just mean that the nullable type does not itself overload operators and will use the operators from the underlying type that it represents?

似乎没有要对SO有关解除运营商多的信息,所以希望这可以帮助一些人出去了。

There doesn't seem to be much information on SO about lifted operators, so hopefully this can help some others out too.

感谢

推荐答案

解禁运营商运营商而工作在可空类型由已非空的形式存在升降的运营商。因此,举例来说,如果你这样做:

Lifted operators are operators which work over nullable types by "lifting" the operators which already exist on the non-nullable form. So for example, if you do:

int? x = 10;
int? y = 10;
int? z = x + y;

这是+操作​​符解除。它实际上并没有对可空&LT存在; INT> 但是C#编译器充当如果是这样,产生code做正确的事情。 (在大多数情况下,这是检验一个操作数是否为空的问题;如果是这样,则结果为空,否则,拆开包装两个操作数其不可为空值,使用正常运营,然后敷结果转换回。空值,大约有比较的一些特殊情况,但。)

That "+" operator is lifted. It doesn't actually exist on Nullable<int> but the C# compiler acts as if it does, generating code to do the right thing. (For the most case, that's a matter of checking whether either operand is null; if so, the result is null. Otherwise, unwrap both operands to their non-nullable values, use the normal operator, and then wrap the result back into a nullable value. There are a few special cases around comparisons though.)

请参阅第6.4.2 C#规范的(提升转换运算符)和7.3.7(解除经营者)了解更多信息。

See section 6.4.2 (lifted conversion operators) and 7.3.7 (lifted operators) of the C# spec for more information.

这篇关于什么是提升运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆