I am surprised that no one pointed out that if you know that the value will be a legal long value, you can change the behavior of the compiler with an explicit application and just use long .
This may or may not be useful, depending on the condition that defines the value for asInt , and depending on what you intend to do with the result of the expression. Here is an example:
string a = "1234.56"; bool asDouble = a.Contains("."); var b = asDouble ? (long)Double.Parse(a) : Int64.Parse(a); Console.WriteLine(b.GetType());
In fact, in this example you do not need a conditional statement; this will work too:
string a = "1234.56"; var b = (long)Double.Parse(a); Console.WriteLine(b.GetType());
In other words, it is possible that the ternary operator will not use the best solution, but the question does not provide enough context to know.
phoog source share