Ambiguous general constraint T: class vs T: struct
This code generates a compiler error when an item is already defined with the same parameter types.
private T GetProperty<T>(Func<Settings, T> GetFunc) where T:class
{
try
{
return GetFunc(Properties.Settings.Default);
}
catch (Exception exception)
{
SettingReadException(this,exception);
return null;
}
}
private TNullable? GetProperty<TNullable>(Func<Settings, TNullable> GetFunc) where TNullable : struct
{
try
{
return GetFunc(Properties.Settings.Default);
}
catch (Exception ex)
{
SettingReadException(this, ex);
return new Nullable<TNullable>();
}
}
Is there a clean job?
a source to share
Generic constraints cannot be used to resolve overloading, but you don't really need an overloaded method for that. Just use default
instead 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, and I copied your code verbatim, but please don't swallow generic Exception
like this. Catch the exception specific , which may be caused in fact . You don't want this code to inadvertently swallow instances of OutOfMemoryException
or BadImageFormatException
.
a source to share