c# - Ambiguous Generic restriction T:class vs T:struct -


this code generates compiler error member defined same parameter types.

   private t getproperty<t>(func<settings, t> getfunc) t:class     {         try         {             return getfunc(properties.settings.default);         }         catch (exception exception)         {             settingreadexception(this,exception);             return null;         }     }      private tnullable? getproperty<tnullable>(func<settings, tnullable> getfunc) tnullable : struct     {         try         {             return getfunc(properties.settings.default);         }         catch (exception ex)         {             settingreadexception(this, ex);             return new nullable<tnullable>();         }     } 

is there clean work around?

generic type constraints can't used overload resolution, don't need overloaded method this. use default instead of null:

private t getproperty<t>(func<settings, t> getfunc) {     try     {         return getfunc(properties.settings.default);     }     catch (exception exception)     {         settingreadexception(this,exception);         return default(t);     } } 

oh, , i've copied code verbatim, please don't swallow general exception this. catch specific exception actually expect might thrown. don't want code inadvertently swallowing outofmemoryexception or badimageformatexception instances.


Comments