Please consider the following snippet:
protected object GetArg(Dictionary<string, object> args, string argName)
{
object arg;
if (args.TryGetValue(argName, out arg))
{
return arg;
}
return null;
}
protected T GetArg<T>(Dictionary<string, object> args, string argName) where T : class
{
return GetArg(args, argName) as T;
}
//following does not compile:
//Type ... already defines a member called 'GetArg' with the same parameter types
protected T GetArg<T>(Dictionary<string, object> args, string argName) where T : struct
{
Nullable<T> arg = GetArg(args, argName) as Nullable<T>;
return arg.HasValue ? arg.Value : default(T);
}
Should not constraining the T parameter type to be mutually exclusive between above generic declarations be enough to guarantee unambiguity? Why are type constraints not considered by the compiler in this case? Is there something I'm missing?