如何访问另一个静态类内部的静态类的成员 [英] How to access member of a static class which is inside another static class

查看:65
本文介绍了如何访问另一个静态类内部的静态类的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级结构如下

public class MainClass
{
    private static class Class1
    {
         private static class Class2
         {
             public const int Id = 2;
         }
    }

    public void getId()
    {
        // I want to access Id here
    }
}

现在我要访问Class2内的变量ID

Now I want to access the variable Id which is inside Class2

我尝试过 Class1.Class2.Id; ,但是它不起作用
我在做什么错了?

I tried like Class1.Class2.Id; But it is not working
What I am doing wrong?

推荐答案

如果要从 Class1 之外访问此文件,则需要将访问修饰符从 private 到 public (可从任何地方访问)或 internal (可从程序集访问).

If you want to access this from outside of the Class1 you need to change the access modifier from private to public (accessible from anywhere) or internal (accessible from the assembly).

public class MainClass
{
    private static class Class1
    {
         // note the modifier change for Class2
         public static class Class2
         {
             public const int Id = 2;
         }
    }

    public void getId()
    {
        var id = Class1.Class2.Id;
    }
}

这篇关于如何访问另一个静态类内部的静态类的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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