nameof在未定泛型(Unbound generic)中的使用
nameof的入参可以是未定泛型(Unbound generic),比如List<>。表达式的结果是List。以前,你需要给泛型指定参数类型。
cs
Console.WriteLine("Hello, World!");
Console.WriteLine("This is a .NET 10.0 application using C# 14.0 features.");
Console.WriteLine($"List<>: {nameof(List<>)}");
Console.WriteLine($"HashSet<>: {nameof(HashSet<>)}");
Console.WriteLine($"Dictionary<,>: {nameof(Dictionary<,>)}");
Console.WriteLine($"Span<>: {nameof(Span<>)}");
Console.WriteLine($"KeyValuePair<,>: {nameof(KeyValuePair<,>)}");
输入结果如下所示。
bash
Hello, World!
This is a .NET 10.0 application using C# 14.0 features.
List<>: List
HashSet<>: HashSet
Dictionary<,>: Dictionary
Span<>: Span
KeyValuePair<,>: KeyValuePair