I have a method that will receive a string
, but before I can work with it, I have to convert it to int
. Sometimes it can be null
and I ha
Why not just use TryParse()
?
public int doSomeWork(string stringValue)
{
int value;
int.TryParse(stringValue, out value);
return value;
}
The above code will return 0
if the value is anything but an actual number.
So in my opinion, my example is the most readable. I try to parse the int and return it. No coalesce operator, and no string methods are used. This method also handles the exceptions that might be thrown when parsing (unless you WANT the exceptions...).