I would try to analyze if it does not work, then try to analyze the value of a larger capacity. If the value of the larger capacity is parsed, then you know that it is out of range. If this fails, then this is a bad input.
string outOfRange = "2147483648"; // +1 over int.MaxValue int result; if (!Int32.TryParse(outOfRange, out result)) { long rangeChecker; if (Int64.TryParse(outOfRange, out rangeChecker)) //out of range else //bad format }
Unfortunately, I do not think there is a way to do this in the general case for any type; you will have to write an implementation for all types. So, for example, what to do for Int64 ? Perhaps use BigInteger instead:
string outOfRange = "9223372036854775808"; // +1 over Int64.MaxValue long result; if (!Int64.TryParse(outOfRange, out result)) { BigInteger rangeChecker; if (BigInteger.TryParse(outOfRange, out rangeChecker)) //out of range else //bad format }
EDIT: double floating point values ββmay be more interesting with AFAIK, there is no "BigDecimal" there, and you may also have to consider values ββthat approach 0 in a pinch (not sure about that). You might be able to make a variation in the BigInteger test, but you might also need to consider decimal points (perhaps a simple regular expression would be best to have only numbers, an optional negative sign, and only one at most decimal point). If there are decimal points, you should truncate them and just check the whole part of the string.
EDITx2: here's a pretty ugly implementation for checking double values:
// +bajillion over Double.MaxValue string outOfRange = "90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.1"; double result; if (!Double.TryParse(outOfRange, out result)) { string bigIntegerInput = outOfRange; if (!Regex.IsMatch(bigIntegerInput, @"^-?[0-9]\d*(\.\d+)?$")) //bad format int decimalIndex = bigIntegerInput.IndexOf('.'); if (decimalIndex > -1) bigIntegerInput = bigIntegerInput.Substring(0, decimalIndex); BigInteger rangeChecker; if (BigInteger.TryParse(bigIntegerInput, out rangeChecker)) //out of range else //bad format }
But honestly, at the moment, I think we have just left the deep end. If you have some kind of real performance bottleneck, or your application has frequently used values ββout of range, you might be better off catching them at odd times, as is the case in this answer, or perhaps more simply by applying regular expression to the input. In my last example, maybe I just left after the regex was executed (but I donβt know, from my point of view, if the TryParse implementation is softer, which allows using exponential / scientific notation, the regex should also cover them)