部分类中同一源文件的变量范围

发布于 2024-11-06 10:42:36 字数 148 浏览 0 评论 0原文

无论如何,是否可以在部分类中重用方法/变量名称?

internal 这样的东西在程序集级别定义了变量范围,但这次是在源文件级别。因此,我们可以在另一个代码文件中使用相同的名称,并且该变量可供同一代码 (*.cs) 文件中的其他成员使用。

Is there anyway to reuse a method/variable name in partial classes?

Something like internal which defines a variable scope at assembly level but this time at source file level. So we can use the same name in another code file and that variable is available to other members at the same code (*.cs) file.

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

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

发布评论

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

评论(3

鲜肉鲜肉永远不皱 2024-11-13 10:42:36

有没有办法重复使用
部分方法/变量名称
课程?

不,因为部分类只是意味着实际的类被分成多个文件。在编译时,它们被组合成一个类,并且应用所有与通常相同的规则。

我不知道你想要做什么的具体细节,但我怀疑你可能有两个不同的类,并且一个类继承另一个类。将方法等标记为内部而不是私有,然后子类可以看到它们,就像它们在同一个类中一样。

如果你绝对需要在子类中使用相同的变量名,你可以使用 new 关键字:new string Foo = "this is a new string."; 这将忽略旧的 Foo 字符串基类并使用您刚刚重新声明的基类。

来自 C# 4.0 规范:

部分方法除外
(§10.2.7),一个成员的集合
多部分声明的类型是
只是成员集的并集
在每个部分中声明。的尸体
类型声明的所有部分
共享相同的声明空间
(§3.3),以及每个成员的范围
(§3.7)延伸到所有的身体
零件。可访问域为
任何成员总是包括所有
封闭式的部件;私人
在某一部分中声明的成员是自由的
可从另一部分访问。它是一个
声明相同的编译时错误
超过一个部分的成员
类型,除非该成员是类型
使用部分修饰符。

partial class A
{
    int x;                      // Error, cannot declare x more than once
    partial class Inner     // Ok, Inner is a partial type
    {
        int y;
    }
}
partial class A
{
    int x;                      // Error, cannot declare x more than once
    partial class Inner     // Ok, Inner is a partial type
    {
        int z;
    }
}

Is there anyway to reuse a
method/variable name in partial
classes?

No, because partial classes just mean that the actual class is split up amongst more than one file. At compile time they are combined into a single class, and all the same rules apply that normally does.

I don't know the specifics of what you are trying to do, but I suspect you could have two different classes and have one inherit from the other. Mark the methods etc. internal instead of private and then the subclass can see them like they were in the same class.

If you absolutely need to use the same variable name in the subclass, you can use the new key word: new string Foo = "this is a new string."; which will ignore the old Foo string in the base class and use the one you have just redeclared.

From the C# 4.0 Spec:

With the exception of partial methods
(§10.2.7), the set of members of a
type declared in multiple parts is
simply the union of the set of members
declared in each part. The bodies of
all parts of the type declaration
share the same declaration space
(§3.3), and the scope of each member
(§3.7) extends to the bodies of all
the parts. The accessibility domain of
any member always includes all the
parts of the enclosing type; a private
member declared in one part is freely
accessible from another part. It is a
compile-time error to declare the same
member in more than one part of the
type, unless that member is a type
with the partial modifier.

partial class A
{
    int x;                      // Error, cannot declare x more than once
    partial class Inner     // Ok, Inner is a partial type
    {
        int y;
    }
}
partial class A
{
    int x;                      // Error, cannot declare x more than once
    partial class Inner     // Ok, Inner is a partial type
    {
        int z;
    }
}
二智少女猫性小仙女 2024-11-13 10:42:36

目前你没有这样的选择。

部分类是语法糖。编译后,所有部分都成为同一类。

Currently you don't have such option.

Partial classes are a syntactic sugar. All parts become the same class once compiled.

奢欲 2024-11-13 10:42:36

不,最难访问的修饰符是 private,并且它将跨越整个类。

你应该真正考虑一下为什么你需要它。我并不以缺乏创造力而闻名(希望如此),但我发现很难想象一个场景,其中这确实是一个要求。这并不奇怪。

No, the least accessible modifier is private, and that will span the whole class.

You should really consider why you would even need that. I am not known for being uncreative (hope so), but I find it hard to picture a scenario, where this is really a requirement. One that isn't all weird, that is.

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