为什么要重载 true 和 false 而不是定义 bool 运算符?

发布于 2024-08-29 03:32:36 字数 849 浏览 5 评论 0原文

我一直在阅读有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌路黄昏 2024-09-05 03:32:36

正如文档所说,重载truefalse 旨在支持(可为空)数据库类型(是/否、Y/N、0/1 等)。

当然,您可以像任何运算符一样对它们进行不一致的定义。您有责任归还合理的东西。编译器仅要求两者都不要求或两者都要求。

As the docs say, overloading true and false 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.

尘曦 2024-09-05 03:32:36

我不知道这些运营商的存在。这意味着您可以实现自我否定悖论:

public class ThisClassIsFalse
{
    public static bool operator true(ThisClassIsFalse statement)
    {
        return statement ? false : true;
    }

    public static bool operator false(ThisClassIsFalse statement)
    {
        return statement ? true : false;
    }
}

所以现在我们知道这个经典悖论的真正解决方案...... StackOverflowException。

I had no idea these operators existed. That means you can implement the self-negation paradox:

public class ThisClassIsFalse
{
    public static bool operator true(ThisClassIsFalse statement)
    {
        return statement ? false : true;
    }

    public static bool operator false(ThisClassIsFalse statement)
    {
        return statement ? true : false;
    }
}

So now we know the true solution to this classic paradox... StackOverflowException.

甜宝宝 2024-09-05 03:32:36

我见过人们重载 truefalse 重载,以便在 Linq 存在之前做一些聪明的事情,例如在 .NET 2.0 中构建表达式。

Ayende 使用他的 NHQG 项目制定了如下语法来构建 NHibernate 标准查询:

return Repository.FindAll(
    (Where.Publisher.Name == name) &&
    (Where.Publisher.City == city));

I've seen people overload the true and false 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:

return Repository.FindAll(
    (Where.Publisher.Name == name) &&
    (Where.Publisher.City == city));
作业与我同在 2024-09-05 03:32:36

根据系统的不同,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文