A question here raised a question for me:
Ternary operations in c#, say x = y ? a : b
, use the type of either a
or b
to determine the type of the ternary expression. Why doesn't it use the type of x
? In any given situation, isn't there an expected return type that it can use?
EDIT: For the sake of clarity, when I say
Why doesn't it use the type of
x
?
I suppose I mean
Why doesn't it first try to use the type of
x
?
As the documentation states:
Could this process start with:
"In any given situation, isn't there an expected return type that it can use?"
No. Consider for example:
string s = Convert.ToString(y ? a : b);
There are multiple overloads of the ToString
method that takes different data types. The compiler needs to know what the type of the expression is to know which overload to use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With