The following doesn’t compile
public static T Retrieve<T>(this NameValueCollection collection, String key) where T : Object
{
if (collection.AllKeys.Contains(key))
{
try
{
val = (T)Convert.ChangeType((object)collection[key], typeof(T));
}
catch { }
}
return val;
}
because the Constraint cannot be the object class. So is there a way to contrain T for anything that can be set to a null?
Your current constraint,
where T : Objectsays “anything which is or inherits from System.Object”, which is: everything. All types, including Int32 and String, inherit from System.Object. So constraining on Object would do nothing.Edit: as usual, Eric shines a light on this in a far more accurate way: