Generics – Default value

I had forgotten about this for some reason and today I needed to use it so in case anyone ever needs to set a generic value to its default, it’s pretty simple:

private T MyGenericMethod(string someValue)
        {
            T _ValueToCompute = default(T);
            .
            .
            .
            return _ValueToCompute;
        }

You can’t just set it to null since the parameter coming in could be of a value type.

Scroll to Top