Willcode4cash icon

Parse<T>

Willcode4cash | PRO | 10/31/16 07:49:56 PM UTC | 0 ⭐ | 304 👁️ | Never ⏰ | []
C# |

510 B

|

None

|

0 👍

/

0 👎

public static T Parse<T>(this string value) {        
    // Get default value for type so if string
    // is empty then we can return default value.
    T result = default(T); 
    if (!string.IsNullOrEmpty(value)) {            
        // we are not going to handle exception here
        // if you need SafeParse then you should create
        // another method specially for that.
        TypeConverter tc = TypeDescriptor.GetConverter(typeof(T)); 
        result = (T)tc.ConvertFrom(value);
        } 
    return result;
}

Comments