如何在C#中为泛型类创建别名?

Hos*_*Aly 36 c# generics syntax

如何在C#中执行以下操作?编写此代码段的第一行的正确方法是什么?

using KVP<K, V> = System.Collections.Generic.KeyValuePair<K, V>;

class C { KVP<int, string> x; }
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 39

基本上你不能.您只能使用固定别名,例如:

using Foo = System.Collections.Generic.KeyValuePair<int, string>;

class C { Foo x; }
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这很不幸 (7认同)