In the following code, I would expect var
to get resolved to an Int64
, but it gets resolved to a double
. Why is it so?
string a = "1234";
bool asInt = true;
var b = (asInt) ? Int64.Parse(a) : Double.Parse(a) ;
Console.WriteLine(b.GetType());
There is an implicit conversion from Int64
to Double
but not the other way (due to possible loss of precision in that direction).
Since both "branches" of the conditional need to resolve to the same type, the type of b
ends up being inferred as a Double
.
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