Search code examples
c#structure

C# static data hierarchy


Is there any way to create a static hierarchy with values? For example there is one "baseclass" named "Layer1", one "subclasses" of Layer one (Layer two) and a static string named value1 (in layer2). My goal is now to access the data via the command:

currentValue = Layer1.Layer2.Value1

so without creating an object of a non static class. You can't extend a static class but is there any workaround?


Solution

  • You don't need to make the class static, only the variable.

    Example:

    public class Class1
    {
        public class Class2
        {
            public static string String1 = "a";
        }
    }
    
    public class OtherClass
    {
        string s = Class1.Class2.String1;
    }