I have a utility method which returns a strongly typed value from an old .INI configuration type file, with the signature
internal static T GetIniSetting<T>(string config, string key, T defVal = default(T)) I want strings to be special, in that I would like the default value for defaultValue to be string.Empty, not default(string) (i.e. null), in the case when the coder hasn't specified a default value.
if (cantFindValueInIniFile == true) { if ((typeof(T) == typeof(string)) && (defaultValue == null)) { // *** Code needed here - Cannot convert string to <T>*** return (T)string.Empty; } return defaultValue; } I've tried hard casting, and the as keyword, to no avail.