It'd be nice if I could define a method like this:
public T GetItem<T>() where T : null
{
if (someCondition<T>())
return someCalculation<T>();
else
return null;
}
Then I could use this on reference types (e.g., object
, string
) as well as nullable value types (e.g., int?
, double?
): anything that can be assigned to null
. But this isn't possible.
Then again, for all I know, maybe it is? I know there'a no null
constraint like I just fantasized about; but is there some workaround or clever way of accomplishing this that just hasn't occurred to me?