I've seen many type converter in C#, but I haven't come to a generic one yet ... so I decide to write one. I hope this will help ... someone out there 
This method provides a unified way of converting object of values to other types.

This method provides a unified way of converting object of values to other types.
/// <summary> /// Converts to the type that you like /// </summary> /// <author>www.pickmike.com</author> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static T convertToType<T>(object value, T defaultValue) { T result = defaultValue; try { if (value != null) { bool tryToCast = true; // Checks for the date if (typeof(T) == typeof(DateTime)) { if (string.IsNullOrEmpty(value.ToString())) { tryToCast = false; } } if (tryToCast) { result = (T)Convert.ChangeType(value, typeof(T)); } } } catch { } return result; }
Usage:
int myInt = convertToType<int>("10", 0);DateTime? myDate = convertToType<DateTime?>("something invalid", null);
aren't there one line ifs in c#?