New in .NET 10.0 [10]: Simplification for nameof() with generic types in C#
When applying nameof() to generic types in C# 14.0, you can omit the type parameters in the code.
(Image: Pincasso/Shutterstock)
1 min. read
By
- Dr. Holger Schwichtenberg
Previously, in C#, you always had to specify concrete type parameters to apply the nameof() operator to generic types. Starting with the current C# 14.0, you can omit the type parameters in the code as a small syntax simplification.
Videos by heise
The following code example shows the simplified use of nameof() with generic types:
// BISHER
Console.WriteLine(nameof(List<int>)); // --> List
Console.WriteLine(nameof(System.Collections.Generic.LinkedListNode<int>)); // --> LinkedListNode
Console.WriteLine(nameof(System.Collections.Generic.KeyValuePair<int, string>)); // --> KeyValuePair
// NEU
Console.WriteLine(nameof(List<>)); // --> List
Console.WriteLine(nameof(System.Collections.Generic.LinkedListNode<>)); // --> LinkedListNode
Console.WriteLine(nameof(System.Collections.Generic.KeyValuePair<,>)); // --> KeyValuePair
(mho)