Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does var get resolved as a Double and not Long?

Tags:

c#

double

int64

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());
like image 694
xbonez Avatar asked Nov 30 '22 03:11

xbonez


1 Answers

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.

like image 164
Oded Avatar answered Dec 09 '22 11:12

Oded