C# 是否可以用.net类型声明别名?

C# 是否可以用.net类型声明别名?,c#,alias,C#,Alias,是否可以用.net类型声明别名?在c#中,如果是,如何进行?是。使用指令 如果你用“c#alias”这个词搜索,你也会很容易找到答案。是的,你可以这样做 using q = System.Int32; 例如: using MatchBuilderFactoryFunc = System.Func< IndexerBase.RequestMatching.RequestFreeTextAnalyzeParameter, System.Collections.Generic.

是否可以用.net类型声明别名?在c#中,如果是,如何进行?

是。使用指令

如果你用“c#alias”这个词搜索,你也会很容易找到答案。

是的,你可以这样做

using q = System.Int32;
例如:

using MatchBuilderFactoryFunc = System.Func<
    IndexerBase.RequestMatching.RequestFreeTextAnalyzeParameter,
    System.Collections.Generic.IEnumerable<
        IndexerBase.RequestMatching.IMatchBuilder>>;

然而,它只在编译一个C#文件时存在。无法导出此别名。

正如其他人所说,请使用using指令的“using alias=type;”形式。关于这一点,有两点:

1) 它必须是文件或名称空间中的第一件事。(当然,除非您有extern alias指令;它们位于using指令之前。)

2) 别名不是真正的类型。很多人希望看到:

using PopsicleCount=System.Int32;
using GiraffeCount=System.Int32;
...
PopsicleCount p = 123;
GiraffeCount g = p; // ERROR, can't convert a count of giraffes into a count of popsicles
但我们不支持该功能。这是一个真实的别名;当我们看到别名标识符时,我们只是用alising类型替换它;p和g都是int类型

3) 别名仅适用于它出现在其中的文件或命名空间声明。如果要在程序中的每个文件中使用别名,则必须在程序中的每个文件中写入别名

4) 别名不能在别名端参数化,尽管它们可以是类型端的封闭泛型类型。也就是说,这是合法的:

using GiraffeList = System.Collections.Generic.List<Giraffe>;
使用GiraffeList=System.Collections.Generic.List;
但这不是:

using Frobtionary<T> = System.Collections.Generic.Dictionary<Frob, T>;
使用Frobtionary=System.Collections.Generic.Dictionary;
这是正确的。显示了一个示例。的可能重复项
using GiraffeList = System.Collections.Generic.List<Giraffe>;
using Frobtionary<T> = System.Collections.Generic.Dictionary<Frob, T>;