簡體   English   中英

如何訪問另一個靜態類內部的靜態類的成員

[英]How to access member of a static class which is inside another static class

我有以下課程結構

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

我試過像Class1.Class2.Id; 但這不起作用
我做錯了什么?

如果要從Class1外部訪問此文件,則需要將訪問修飾符從private更改為public (可從任何地方訪問)或internal (可從程序集訪問)。

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM