Strange order of execution,There's no short circuit. What causes this? #8288
-
public class TestClass
{
//change to int
public static implicit operator int(TestClass c)
{
if (c == null)
{
throw new Exception();
}
return 1;
}
}
public class Test
{
public TestClass TestClass { get; set; }
[Test]
public void TestMethod()
{
int integer = false ? TestClass : default;
Console.WriteLine(integer);//will throw Exception!
}
}
|
Beta Was this translation helpful? Give feedback.
Answered by
HaloFour
Jul 16, 2024
Replies: 2 comments 8 replies
-
With local type inference, the type of var integer = false ? TestClass : default; Did you mean? int integer = false ? TestClass : default; |
Beta Was this translation helpful? Give feedback.
5 replies
-
i want to know why not circuit? |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The expression does short-circuit. That's not the problem. The issue is that the expression by itself is evaluated before the assignment, which is when the conversion operation occurs. The conditional expression
false ? TestClass : default
evaluates to aTestClass
, not anint
, and thedefault
value of aTestClass
isnull
.