C# Generics – Understanding Class? in Type Constraints

c#-8.0c++genericsnullable-reference-types

While I tried to find an answer to another question I noticed that this code compiles in C#:

public void Foo<T>(T obj)
    where T : class?
{
}

I did not manage to find in the documentation what it even means.

Best Answer

It enforces that T has to be a nullable reference type.

The type you set in for T, must derive from object?.

It's a new feature in C#8, to explictly declare a type as nullable. if you have

 Add<T>(T tmp);

You document, it's OK to Add null;