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.

listen Print view

(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.

The Dotnet Doctor – Holger Schwichtenberg
Der Dotnet-Doktor – Holger Schwichtenberg

Dr. Holger Schwichtenberg is the technical director of the expert network www.IT-Visions.de, which supports numerous medium-sized and large companies with consulting and training services as well as software development, drawing on the expertise of 53 renowned experts. Thanks to his appearances at numerous national and international conferences, as well as more than 90 specialist books and over 1,500 specialist articles, Holger Schwichtenberg is one of the best-known experts for .NET and web technologies in Germany.

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)

Don't miss any news – follow us on Facebook, LinkedIn or Mastodon.

This article was originally published in German. It was translated with technical assistance and editorially reviewed before publication.