Nullable types in generic class

Viewed 22631

I need to create a class similar to this:

class GenericClass<T>
{
    public T[] Arr {get; }
    public GenericClass(int n)
    {
        Arr = new T[n];
        for (int i = 0; i < n; i++)
        {
            Arr[i] = null;
        }
    }
}

But there is a compiler error:

CS0403 Cannot convert null to type parameter 'T' because it could be a non-nullable value type. Consider using 'default(T)' instead.

I don't want to use default(T), because it could be the same as normal value but I need to distinguish. I don't know the type so I can't use minimal value. How can I use null?

4 Answers
Related