Elegant one-line way to use TryParse

added by DotNetKicks
4/17/2018 1:03:15 PM

1 Kicks, 257 Views

I don't know how about you, but for me using int.TryParse() always feel somehow not right, resulting in an ugly three lines of code to do just one operation: int value; if (!int.TryParse(valueToParseFromString, out value) value = 0; If you are looking for cleaner code to satisfy your inner pedant you can use this one-line solution instead: int value = int.TryParse(valueToParseFromString, out value) ?