[In response to Solution 2]
In my comment, I tried to explain what's wrong with it. You simply don't know generic solution, so my solution is not even directly related to your original question. The only reason for my advices is: it should "protect" you from wrong thinking shown in Solution 2. Let's see.
In principle, this will compile and execute:
string GetName<t>(T value) {
if (value == null) { }
return value.ToString();
}</t>
Suppose you really need
T
to be generic. I case
T
is a value type, the expression
value == null
is always false, so what is really check up. The type is not nullable, so null is not really applicable, even though the check is always valid formally. To see that
null
is inapplicable, try
value = null;
The only "universal" check you can do on all types is this
if (value == default(T)) { }
It will still check for
null
for generic types; for other types, it will check for some defaults: 0 for integer types, and the like.
—SA