Microsoft DN
Microsoft DN

Reputation: 10020

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

I have following class structure

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

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

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

Upvotes: 0

Views: 81

Answers (1)

RaYell
RaYell

Reputation: 70414

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

Upvotes: 5

Related Questions