Null checking a generic type Assuming I have a method with this signature

public void DoStuffWith&lt;T&gt;(T foo)


and I want to allow both structs and classes to be passed as T, I was wondering about how to best do the null-checking in case T is a class.

I came up with following two approaches:



1. Checking whether T is a struct or a class at runtime:

public void DoStuffWith&lt;T&gt;(T foo)
{
    if(!typeof(T).IsValueType &amp;&amp; foo == null)
    {
        throw new ArgumentNullException(nameof(foo));
    }

    ...
}


2. Having two overloads with generic type constraints:

public void DoStuffWithStruct&lt;T&gt;(T foo) where T : struct
{
    ...
}

public void DoStuffWithClass&lt;T&gt;(T foo) where T : class
{
    if(foo == null)
    {
        throw new ArgumentNullException(nameof(foo));
    }

    ...
}




However, I dislike both approaches. I don't like the runtime type-checking at #1 and I don't like having multiple, differently named overloads at #2.

Which of the approaches do you suggest or do you think I should maybe consider a totally different approach altogether?

I appreciate any feedback.
If you try the following :

class Program
{
    static void Main(string[] args)
    {
        Foo&lt;string&gt;(null);
        Foo&lt;string&gt;("asd");
        Foo&lt;int&gt;(1);
        Console.ReadKey();
    }

    public static void Foo&lt;T&gt;(T test)
    {
        if(test == null)
        {
            Console.WriteLine("ho ho it's null");
            return;
        }

        Console.WriteLine(test.GetType());
    }
}


You will realize that both the options are, excuse me, useless. The language already supports this behavior and if T is a struct, test == null will simply return false all the time.