C# Feature - nameof with unbound generic types
nameof with unbound generic types
Section titled “nameof with unbound generic types”Beginning with C# 14, the argument to nameof can be an unbound generic type such as List<> or Dictionary<,>.
Introduced In
Section titled “Introduced In”This feature was introduced in C# 14. The official Microsoft Learn page for C# 14 is dated November 18, 2025 and states that C# 14 ships with .NET 10 and Visual Studio 2026.
Why This Feature Exists
Section titled “Why This Feature Exists”Before C# 14, nameof worked well for identifiers and closed generic types, but not for unbound generic type forms. That gap was awkward in analyzers, source generation, diagnostics, and reflection-heavy infrastructure code where the generic type definition itself matters.
Example
Section titled “Example”Console.WriteLine(nameof(List<>)); // ListConsole.WriteLine(nameof(Dictionary<,>)); // DictionaryWhen To Use It
Section titled “When To Use It”- Use it when logging or reporting against a generic type definition rather than a constructed type.
- Use it in code generation and analyzer scenarios where open generic types are part of the model.
- Prefer
nameofover string literals whenever the name should follow refactoring automatically.
Common Pitfalls
Section titled “Common Pitfalls”nameof(List<>)returnsList, notList<>.- This feature is about compile-time names, not runtime type inspection.