为什么要重载 true 和 false 而不是定义 bool 运算符?
我一直在阅读有关 C# 中重载 true 和 false 的内容,我想我理解了这与定义 bool 运算符之间的基本区别。我看到的例子是这样的:
public static bool operator true(Foo foo) {
return (foo.PropA > 0);
}
public static bool operator false(Foo foo) {
return (foo.PropA <= 0);
}
对我来说,这等同于说:
public static implicit operator bool(Foo foo) {
return (foo.PropA > 0);
}
据我所知,区别在于,通过分别定义 true 和 false,你可以拥有一个既 true 又 false 的对象,或者既不正确也不错误:
public static bool operator true(Foo foo) { return true; }
public static bool operator false(Foo foo) { return true; }
//or
public static bool operator true(Foo foo) { return false; }
public static bool operator false(Foo foo) { return false; }
我确信这是允许的,但我就是想不出它是什么。对我来说,如果您希望一个对象能够转换为 true 或 false,那么单个 bool 运算符最有意义。
谁能给我一个以其他方式进行有意义的场景?
谢谢
I've been reading about overloading true and false in C#, and I think I understand the basic difference between this and defining a bool operator. The example I see around is something like:
public static bool operator true(Foo foo) {
return (foo.PropA > 0);
}
public static bool operator false(Foo foo) {
return (foo.PropA <= 0);
}
To me, this is the same as saying:
public static implicit operator bool(Foo foo) {
return (foo.PropA > 0);
}
The difference, as far as I can tell, is that by defining true and false separately, you can have an object that is both true and false, or neither true nor false:
public static bool operator true(Foo foo) { return true; }
public static bool operator false(Foo foo) { return true; }
//or
public static bool operator true(Foo foo) { return false; }
public static bool operator false(Foo foo) { return false; }
I'm sure there's a reason this is allowed, but I just can't think of what it is. To me, if you want an object to be able to be converted to true or false, a single bool operator makes the most sense.
Can anyone give me a scenario where it makes sense to do it the other way?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如文档所说,重载
true
和false
旨在支持(可为空)数据库类型(是/否、Y/N、0/1 等)。当然,您可以像任何运算符一样对它们进行不一致的定义。您有责任归还合理的东西。编译器仅要求两者都不要求或两者都要求。
As the docs say, overloading
true
andfalse
is intended to support (nullable) database-types (Yes/No, Y/N, 0/1, etc).And of course you can define them inconsistently, as with any operator. It is your responsibility to return something sensible. The compiler goes no further than requiring neither or both.
我不知道这些运营商的存在。这意味着您可以实现自我否定悖论:
所以现在我们知道这个经典悖论的真正解决方案...... StackOverflowException。
I had no idea these operators existed. That means you can implement the self-negation paradox:
So now we know the true solution to this classic paradox... StackOverflowException.
我见过人们重载
true
和false
重载,以便在 Linq 存在之前做一些聪明的事情,例如在 .NET 2.0 中构建表达式。Ayende 使用他的 NHQG 项目制定了如下语法来构建 NHibernate 标准查询:
I've seen people overload the
true
andfalse
overloads in order to do clever things like building expressions in .NET 2.0, before Linq existed.Ayende worked out a syntax like this to build NHibernate criteria queries, using his NHQG project:
根据系统的不同,true 可以是任何非零值。在其他情况下,它可以是任何正值。
其他系统并不是真正的布尔值,并且允许布尔值有第三种状态 null 或 nill,这就是为什么您可能会重载 true 和 false,而不是重载单个 bool 运算符。
Depending on the system, true can be any non-zero value. In others, it can be any positive value.
Other systems aren't truly boolean, and allow a third state null or nill for the boolean values, which is why you might overload true and false, versus overloading a single bool operator.