包含csharp中javascript等效信息的对象

时间:2017-04-04 16:07:06

标签: javascript c# object

在javascript中,我使用并创建对象来存储有关我的程序的信息,如下所示:

var information = {
    page1 : {
        info : "this is some information about page 1",
        time : 1491319773386
    },
    page2 : {
        info : "this is some information about page 2",
        time : 1491319796452
    }
};

csharp中是否有等价物?
像这样:

class Test {
    public static Class pages = Class {
        public static Class page1 = Class {
            public static string info = "this is page1";
            public static int date = 14487989879987;
        }
        public static Class page2 = Class {
            public static string info = "this is page2";
            public static int date = 14487989845344;
        }
    }
}

用法:

class Main {
    public static void main(string[] args) {
        Console.WriteLine(Test.pages.page1.info);
    }
}

2 个答案:

答案 0 :(得分:0)

为此我们通常使用类。 上课非常简单。

public class Information { 
    public string[] page1 = new string[2]{"this is ...", "..."}; 
    public string[] page2 = new string[2]{"aaaa", "bbbb"}; 
}

你不一定需要使用string [] tho,在C#中有很多方法可以做到这一点。我希望这就是你要求的!

此致 realmaster42

答案 1 :(得分:0)

这可以使用彼此嵌套的static类来完成:

public static class Test {
    public static class pages {
        public static class page1 {
            public static string info = "this is page1";
            public static int date = 14487989879987;
        }
        public static class page2 {
            public static string info = "this is page2";
            public static int date = 14487989845344;
        }
    }
}

这与您的示例相似,但更加整洁有效。