内部类和外部类在C# [英] Inner class and Outer class in c#

查看:87
本文介绍了内部类和外部类在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

how to implement inner outer classes in c#

我有两个嵌套类

class Outer
{
    int TestVariable = 0;
    class Inner
    {
        int InnerTestVariable = TestVariable // Need to access the variable "TestVariable" here
    }
}

其示值误差在编译时。

它可以通过

1)使TestVariable静态

1) Making TestVariable as static

2)传递​​外部类的一个实例内部类

2) Passing an instance of Outer class to Inner class

而在的Java 就没有必要创建实例或静态的。

but in java there is no need to create Instance or static .

我可以使用C#相同的功能呢?

can i use the same functionality in C# too ?

推荐答案

没有,C#不具备在这种情况下,相同的语义的Java。你可以让 TestVariable 常量静态,或通的实例内蒙古构造因为你已经注意到。

No, C# does not have the same semantics as Java in this case. You can either make TestVariable const, static, or pass an instance of Outer to the constructor of Inner as you already noted.

这篇关于内部类和外部类在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆