save time with a simple conversion that works for all nullable Types
For a while, I have used a ToNullable method (that I found somewhere on the intertubes) that required the input of a TryParse delegate like this:
int? eight = "8".ToNullable<T>(int.TryParse);
int? nInt = "".ToNullable<T>(int.TryParse);//null
Which wasn't bad in any way, but I realized that every time I was using this method, I would have to type in the TryParse of the Type I was trying to get; clearly there is a better way, and I found it using TypeConverter.
Now I can use my new ToNullable method in a cleaner, less repetitive way:
int? eight = "8".ToNullable<T>();
int? nInt = "".ToNullable<T>();//null
Here is the code:
public static Nullable<T>
ToNullable<T>(this string s) where T : struct
T? result = null;
TypeConverter converter = TypeDescriptor
result = (T?)converter.ConvertFrom(s);
This has been added to my Utilities Library on CodePlex.