如何在C#中创建整个应用程序中存在的自定义类的常量数组?

时间:2017-05-10 21:54:55

标签: c# arrays class

我是C#的新手,正在尝试编写一种纸牌游戏。我创建了一个包含每张卡独有的所有信息的类,我想在运行时创建一个数组,该数组将保持不变并可供所有方法使用。我可以用一个类做这个,如果有的话,我在哪里创建我的数组?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这里有3种可能的解决方案,各有所不同,各有利弊:

class Program
    {
        public static readonly int[] Info1 = new int[44];
        public static readonly List<string> Info2 = new List<string>();
        public static readonly ReadOnlyCollection<int> Info3 = new ReadOnlyCollection<int>(new int[] { 1, 2 });

您可以通过Program.InfoX访问InfoX,例如