方法中类名的别名

发布于 2024-09-24 18:19:10 字数 125 浏览 0 评论 0原文

我有一些棘手的问题..
我可以在文档顶部为类定义别名,例如

using MyName = Bla.Bla.ClassName

但是我可以在方法中定义类似的东西吗?

I have some curly question..
I can define alias for the class at top of my document, Such as

using MyName = Bla.Bla.ClassName

But can I define something like this in the method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱々樱花落 2024-10-01 18:19:10

不,你不能。如果你的意思是你想定义
方法内的类别名,这是不可能的。

别名只能在文件或命名空间级别定义:

using MyName = Bla.Bla.ClassName;
namespace A{
...
}

或者

namespace A{
    using MyName = Bla.Bla.ClassName
...
}

如果您想定义“方法别名”,这也是不可能的:别名仅适用于类型或命名空间。

No, you can't. If you mean that you want to define
a class alias inside a method, this is not possible.

Alias could be defined only at file or namespace level:

using MyName = Bla.Bla.ClassName;
namespace A{
...
}

or

namespace A{
    using MyName = Bla.Bla.ClassName
...
}

If you instead mean you want to define a "method alias", this also isn't possible: alias are only for types or namespaces.

深者入戏 2024-10-01 18:19:10

不可以,using 指令必须位于任何声明之外,或者位于命名空间声明内:

using Foo;
namespace Bar
{
   using Baz;
}

您不能在方法内执行此操作。为什么不直接为全班同学做呢?为什么您希望它在特定方法中应用?

No, using directives have to either be outside any declaration, or within a namespace declaration:

using Foo;
namespace Bar
{
   using Baz;
}

You can't do this within a method. Why not just do it for the whole class though? Why do you only want it to apply within a particular method?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文