[英]How can I create an alias for a generic class in C#?
How can I do the following in C#? 如何在C#中执行以下操作? What is the right way to write the first line of this code snippet?
编写此代码段的第一行的正确方法是什么?
using KVP<K, V> = System.Collections.Generic.KeyValuePair<K, V>;
class C { KVP<int, string> x; }
You can't, basically. 基本上你不能。 You can only use fixed aliases, such as:
您只能使用固定别名,例如:
using Foo = System.Collections.Generic.KeyValuePair<int, string>;
class C { Foo x; }
In some cases you can go with inheritance: 在某些情况下,您可以继承:
public class MyList<T1, T2> : List<Tuple<IEnumerable<HashSet<T1>>, IComparable<T2>>> { }
public void Meth()
{
var x = new MyList<int, bool>();
}
Though not in your particular case, as KeyValuePair is sealed :-( 虽然不是在你的特定情况下,因为KeyValuePair是密封的:-(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.