Nullable types have a lot of benefits when it comes to articulating system design.

However, obtaining information about a nullable type is generally quite an exercise.

Take this example: a nullable int.

typeof(int?);

If we wanted to get some information about it, we would do it like this, using reflection:

Type nullableIntType = typeof(int?);

Console.WriteLine(nullableIntType.Name);

This prints the rather unhelpful:

Nullable`1

To get what it is requires a bit more work:

Type? underlying = Nullable.GetUnderlyingType(nullableIntType);
Console.WriteLine(underlying.Name)

We need to call the GetUnderlyingType method from the Nullable class.

This now prints what we expect:

Int32

This has been improved in .NET 11, where the Type class has gained this functionality directly via the new GetNullableUnderlyingType method.

The code is now more direct:

Type nullableIntType = typeof(int?);
Type? underlying = nullableIntType.GetNullableUnderlyingType();
Console.WriteLine(underlying.Name);

The results will look like this:

nullableImprovements

TLDR

You can now interrogate the Type directly to get information about the underlying type for nullable types.

The code is in my GitHub.

Happy hacking!