c#中的内部类和外部类

Tho*_*eld 5 c#

如何在c#中实现内部外部类

我有两个嵌套类

喜欢

class Outer
{
    int TestVariable = 0;
    class Inner
    {
        int InnerTestVariable = TestVariable // Need to access the variable "TestVariable" here
    }
}
Run Code Online (Sandbox Code Playgroud)

它在编译时显示错误.

它可以解决

1)使TestVariable成为静态的

2)将外类的实例传递给Inner类

但是在java中,不需要创建Instance或static.

我可以在C#中使用相同的功能吗?

Dea*_*ing 11

不,在这种情况下,C#与Java没有相同的语义.你可以制作TestVariable const,static或者传递一个你已经注意到Outer的构造函数的实例Inner.