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 operatortrue
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), orfor-statement
(§8.8.3) is aboolean-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, whereT
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 typeT
, and must return a result of typeT
.T
must contain declarations of operatortrue
and operatorfalse
.
所以
&&
如果您还声明 &
,则可以在您的自定义类型上使用.编辑:
刚刚找到 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 asT.false(x) ? x : T.&(x, y)
, whereT.false(x)
is an invocation of the operatorfalse
declared inT
, andT.&(x, y)
is an invocation of the selected operator&
. In other words,x
is first evaluated and operatorfalse
is invoked on the result to determine ifx
is definitelyfalse
. Then, ifx
is definitelyfalse
, the result of the operation is the value previously computed forx
. Otherwise,y
is evaluated, and the selected operator&
is invoked on the value previously computed forx
and the value computed fory
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 valuetrue
to indicate that its operand is definitely true. Thefalse
operator returns the bool valuetrue
to indicate that its operand is definitely false.
它基本上适用于您希望自定义类型具有真/假值的情况。
LaunchStatus
输入相同的链接和 DBBool
类型 here是很好的例子。
关于c# - c# 中的 "operator true"是否正好有两个可以使用的地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60674675/