问题描述
我希望我的文档错过了这个。是否有申报在C#中类型同义词的方法吗?
I hope I missed this in the docs. Is there a way to declare a type synonym in C#?
推荐答案
您可以使用using语句来为一个类型创建别名
You can use the using statement to create an alias for a type.
例如,下面将创建一个名为为 System.Int32
别名敏
For example, the following will create an alias for System.Int32
called MyInt
using MyInt = System.Int32;
另外,你可以使用继承在某些情况下提供帮助。例如:
Alternatively, you can use inheritance to help in some cases. For example
创建类型为人
这是一个列表<人>
public class People: List<Person>
{
}
不太别名,但它确实简化事情,尤其是对于更复杂的类型,如本
Not quite an alias, but it does simplify things, especially for more complex types like this
public class SomeStructure : List<Dictionary<string, List<Person>>>
{
}
和现在你可以使用类型 SomeStructure
,而不是好玩一般声明。
And now you can use the type SomeStructure
rather than that fun generic declaration.
有关你在你的意见,例如用于元组
,你可以不喜欢以下
For the example you have in your comments, for a Tuple
you could do something like the following.
public class MyTuple : Tuple<int, string>
{
public MyTuple(int i, string s) :
base(i, s)
{
}
}
这篇关于声明在C#中的类型代名词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!