c# - c# 中的 "operator true"是否正好有两个可以使用的地方?

标签 c# boolean operator-overloading

c# 允许您覆盖类的“operator true”和“operator false”:

class Foo
{
   bool Thing;

   Foo(bool thing)
   {
      Thing = thing;
   }

   public static bool operator true(Foo foo) => foo.Thing;
   public static bool operator false(Foo foo) => !foo.Thing;
}

它有点工作。你可以说
Foo foo = new Foo(true);
if (foo)
   Stuff();
string s = foo ? "yes" : "no";

但你不能说
Foo foo = new Foo(true);
bool boo = true;
if (boo && foo)
   Stuff();
if (boo & foo)
   Stuff();
if (boo & (foo == true))
   Stuff();
if (boo & ((bool)foo))
   Stuff();
Foo foo2 = new Foo(true);
if (foo && foo2)
   Stuff();
if (foo & foo2)
   Stuff();
if (foo == boo)
   Stuff();
if (foo != boo)
   Stuff();
bool boo2 = foo;

在每种情况下,编译器都会提示。

除了那些非常具体的语法之外,c# 编译器是否在任何地方使用“operator true”和“operator false”?

编辑

我又找到了一个“operator true”和“operator false”起作用的地方。如果您定义了一个“operator&”,除了真假运算符之外还返回 Foo,编译器将采用表达式“foo1 && foo2”并假装您编写了“foo1 & foo2”,因此调用您的重写运算符。换句话说,“operator true”和“operator false”的存在改变了编译器的行为,即使它从不调用这些运算符。

最佳答案

Does “operator true” in c# have exactly two places it can be used?



不完全是。您可以在 C# Language Specification 上搜索对于“operator true”(我做了),看看它做了什么。栏目 7.12.2 , 7.14 , 7.20提到它。 7.14 本质上是关于你已经知道的三元运算符,但在 7.20 中,它说

A boolean-expression is an expression that yields a result of type bool; either directly or through application of operator true in certain contexts as specified in the following.

The controlling conditional expression of an if-statement (§8.7.1), while-statement (§8.8.1), do-statement (§8.8.2), or for-statement (§8.8.3) is a boolean-expression.



所以,不仅仅是在 if声明,而且还在while , do , for以及。

在 7.12.2 中,它说:

When the operands of && or || are of types that declare an applicable user-defined operator & or operator |, both of the following must be true, where T is the type in which the selected operator is declared:

  • The return type and the type of each parameter of the selected operator must be T. In other words, the operator must compute the logical AND or the logical OR of two operands of type T, and must return a result of type T.
  • T must contain declarations of operator true and operator false.


所以&&如果您还声明 &,则可以在您的自定义类型上使用.

编辑:

刚刚找到 this link ,总结的很清楚。

In other words, the existence of "operator true" and "operator false" change the compiler's behavior even though it never calls those operators.



它确实调用了这些运算符。根据语言规范 7.12.2:

The operation x && y is evaluated as T.false(x) ? x : T.&(x, y), where T.false(x) is an invocation of the operator false declared in T, and T.&(x, y) is an invocation of the selected operator &. In other words, x is first evaluated and operator false is invoked on the result to determine if x is definitely false. Then, if x is definitely false, the result of the operation is the value previously computed for x. Otherwise, y is evaluated, and the selected operator & is invoked on the value previously computed for x and the value computed for y to produce the result of the operation.



基本上,因为 &&短路,它必须知道它的操作数之一是否为假,通过使用 false运算符(operator)。

so why did the language designers create "operator true" and "operator false"?



解释的很好here , 我认为:

The true operator returns the bool value true to indicate that its operand is definitely true. The false operator returns the bool value true to indicate that its operand is definitely false.



它基本上适用于您希望自定义类型具有真/假值的情况。 LaunchStatus输入相同的链接和 DBBool类型 here是很好的例子。

关于c# - c# 中的 "operator true"是否正好有两个可以使用的地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60674675/

相关文章:

c# - 检查 C# new() 初始化是否为 null?

c# - 批量上传大量图像到Azure Blob存储

powershell - Powershell等同于Linux true命令

c++ - 了解 C++ 中的删除

javascript - 我的按钮 id 在 javascript 中调用时有效,但 onserverclick 事件无法在 sql 上存储和更新数据

c# - 什么时候优化不成熟?

java - 我无法让我的程序打印出闰年

mysql - 从 MySQL 结果分配 BOOL

swift - 具有关联值的 swift 枚举中的相等运算符重载

c++ - 如何在 C++ 中同时重载 = 和 [] 运算符