对嵌套类型定义使用指令
Using directive for nested type definitions
我正在尝试使用 using
指令以功能方式定义类型,以使代码更具可读性。假设我的 example.cs 文件如下所示:
using A = System.Tuple<int, int>;
using B = List<A>;
我收到错误:
CS0246 The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)
我可以用 using
定义嵌套类型吗?
docs.microsoft.com 上的 C# 语言规范有一节涵盖 Using directive, specifically the type of using directive you're attempting to use is a "Using alias directive":
A using_alias_directive (Using alias directives) introduces an alias for a namespace or type.
文档突出显示了 using_alias_directive 的允许结构,如:
using_alias_directive
: 'using' identifier '=' namespace_or_type_name ';'
;
请注意,这只允许使用 "namespace_or_type_name",但不允许使用另一个 "using_alias_directive"。可能的原因之一,这可以通过他们的文档部分找到(强调我的):
The order in which using_alias_directives are written has no significance, and resolution of the namespace_or_type_name referenced by a using_alias_directive is not affected by the using_alias_directive itself or by other using_directives in the immediately containing compilation unit or namespace body. In other words, the namespace_or_type_name of a using_alias_directive is resolved as if the immediately containing compilation unit or namespace body had no using_directives
简而言之,恐怕您不能在另一个别名中使用别名。
我正在尝试使用 using
指令以功能方式定义类型,以使代码更具可读性。假设我的 example.cs 文件如下所示:
using A = System.Tuple<int, int>;
using B = List<A>;
我收到错误:
CS0246 The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)
我可以用 using
定义嵌套类型吗?
docs.microsoft.com 上的 C# 语言规范有一节涵盖 Using directive, specifically the type of using directive you're attempting to use is a "Using alias directive":
A using_alias_directive (Using alias directives) introduces an alias for a namespace or type.
文档突出显示了 using_alias_directive 的允许结构,如:
using_alias_directive
: 'using' identifier '=' namespace_or_type_name ';'
;
请注意,这只允许使用 "namespace_or_type_name",但不允许使用另一个 "using_alias_directive"。可能的原因之一,这可以通过他们的文档部分找到(强调我的):
The order in which using_alias_directives are written has no significance, and resolution of the namespace_or_type_name referenced by a using_alias_directive is not affected by the using_alias_directive itself or by other using_directives in the immediately containing compilation unit or namespace body. In other words, the namespace_or_type_name of a using_alias_directive is resolved as if the immediately containing compilation unit or namespace body had no using_directives
简而言之,恐怕您不能在另一个别名中使用别名。