Understanding When Type.FullName Returns Null in .NET
A colleague recently asked me an interesting question: "Why does Type.FullName
return null
in certain situations?" Indeed, the method signature is
C#
public abstract string? FullName { get; }
This behavior might seem unexpected, but there are specific scenarios where the .NET runtime cannot generate a valid full name for a type. Here are the two main cases where Type.FullName
returns null
:
#Generic Types with Open Type Parameters
When you create a generic type that contains unbound generic parameters:
C#
var list = typeof(IList<>);
var dict = typeof(IDictionary<,>);
var listOfDictionaries = list.MakeGenericType(dict); // IList<IDictionary<,>>
Assert.Null(listOfDictionaries.FullName);
#Function Pointers
Function pointer types introduced in C# 9 also have null
as their FullName
:
C#
var functionPointerType = typeof(delegate*<int, void>);
Assert.Null(functionPointerType.FullName);
Do you have a question or a suggestion about this post? Contact me!
Enjoy this blog?
💖 Sponsor on GitHub