int.TryParse
is probably a tad easier:
public static int? ToNullableInt(this string s)
{
int i;
if (int.TryParse(s, out i)) return i;
return null;
}
Edit @Glenn int.TryParse
is “built into the framework”. It and int.Parse
are the way to parse strings to ints.