如何在C#中使用别名类型作为泛型类型参数 [英] How to use an alias type as a generic type parameter in C#
问题描述
如何将使用
定义的别名类型传递给泛型类?
How do you pass an alias type defined by using
to the generic class?
我试过以下代码:
I tried the following code:
using ID = Int32; // it might be replaced with `String`.
using CC = C<ID>;
public class C<T> {
T id;
}
会出现错误:
and there will be an error:
错误CS0246无法找到类型或名称空间名称'ID'(
是否缺少using指令或程序集引用?)
Error CS0246 The type or namespace name 'ID' could not be found (are you missing a using directive or an assembly reference?)
但是using指令正好在发生错误的行的上方。
我错过了什么?
But the using directive is right above the line where the error occurs. Did I miss something?
推荐答案
我并不真正知道你想用这个做什么,但是你但是,可以做类似的继承
I do not really know what you are trying to achieve with this, but you can, however, do something similar with inheritance
public class CC : C<ID>
{
}
类 CC
将从具体类型的
C< T>
派生。因此, CC.T
将会是 Int32
(或其他)。
The class CC
will derive from C<T>
with a concrete type. Hence CC.T
will be of type Int32
(or whatever).
这篇关于如何在C#中使用别名类型作为泛型类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!