public static int? ToNullableInt(this string s) >{ > int result; > bool b = Int32.TryParse(s,out result); > return b ? (int?) result : null; >}Then anywhere this method is in scope you could use, for example:
string s = "12345"; >int? i = s.ToNullableInt();Note the above implementation would return null for any string that doesn't translate to a valid int (null, string.empty, "123.45", "asd" etc.) which may no be exactly what you want. And I wouldn't bother to implement this unless you know it will have a wider use than in your original requirement....