注意:这似乎已在Roslyn中修复
这个问题是在写我对这个问题的回答时出现的,它谈到了null-coalescing operator的关联性。
提醒一下,null-coalescing 运算符的想法是形式的表达式
x ?? y
首先评估x
,然后:
- 如果 的值为
x
空,y
则进行评估,这是表达式的最终结果 - 如果 的值不
x
为空,y
不计算,并且 的值是表达式的最终结果,则在必要时转换为编译时类型x
y
现在通常不需要转换,或者它只是从可空类型到不可空类型 - 通常类型是相同的,或者只是从(比如说)int?
到int
. 但是,您可以创建自己的隐式转换运算符,并在必要时使用这些运算符。
对于 的简单情况x ?? y
,我没有看到任何奇怪的行为。但是,(x ?? y) ?? z
我看到一些令人困惑的行为。
这是一个简短但完整的测试程序 - 结果在评论中:
using System;
public struct A
{
public static implicit operator B(A input)
{
Console.WriteLine("A to B");
return new B();
}
public static implicit operator C(A input)
{
Console.WriteLine("A to C");
return new C();
}
}
public struct B
{
public static implicit operator C(B input)
{
Console.WriteLine("B to C");
return new C();
}
}
public struct C {}
class Test
{
static void Main()
{
A? x = new A();
B? y = new B();
C? z = new C();
C zNotNull = new C();
Console.WriteLine("First case");
// This prints
// A to B
// A to B
// B to C
C? first = (x ?? y) ?? z;
Console.WriteLine("Second case");
// This prints
// A to B
// B to C
var tmp = x ?? y;
C? second = tmp ?? z;
Console.WriteLine("Third case");
// This prints
// A to B
// B to C
C? third = (x ?? y) ?? zNotNull;
}
}
所以我们有三种自定义值类型,A
和B
,C
以及从 A 到 B、A 到 C 和 B 到 C 的转换。
我可以理解第二种情况和第三种情况......但是为什么在第一种情况下会有额外的 A 到 B 转换?特别是,我真的希望第一种情况和第二种情况是同一件事——毕竟它只是将一个表达式提取到一个局部变量中。
有没有人知道发生了什么?当谈到 C# 编译器时,我非常犹豫要喊“错误”,但我对发生的事情感到困惑......
编辑:好的,这是一个更糟糕的例子,感谢配置器的回答,这让我有更多理由认为这是一个错误。编辑:该示例现在甚至不需要两个空合并运算符...
using System;
public struct A
{
public static implicit operator int(A input)
{
Console.WriteLine("A to int");
return 10;
}
}
class Test
{
static A? Foo()
{
Console.WriteLine("Foo() called");
return new A();
}
static void Main()
{
int? y = 10;
int? result = Foo() ?? y;
}
}
这个的输出是:
Foo() called
Foo() called
A to int
Foo()
在这里被调用两次的事实让我非常惊讶——我看不出有任何理由让表达式被计算两次。