如何静态声明对象数组

发布于 2024-11-09 02:01:46 字数 260 浏览 0 评论 0原文

我是一名资深 C 程序员,但对 C# 还很陌生。我想声明一个对象,然后创建一个该对象的数组,静态填充它(我有一个非常大的表要输入)。例如,

class MyObject {
    int i1;
    string s1;
    double d1;
};

static MyObject[] myO = new MyObject {{1,"1",1.0}, {2,"2",2.0}};

这不起作用,但你明白了。任何帮助表示赞赏。

I'm a long time C programmer but new to C#. I want to declare an object then creating an array of that object filling it statically (I have a very large table to enter). For example

class MyObject {
    int i1;
    string s1;
    double d1;
};

static MyObject[] myO = new MyObject {{1,"1",1.0}, {2,"2",2.0}};

This doesn't work but you get the idea. Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

蹲在坟头点根烟 2024-11-16 02:01:46

假设您的班级有公共字段/属性:

class MyObject
{
    public int i1 { get; set; }
    public string s1 { get; set; }
    public double d1 { get; set; }
}   // note: no semicolon needed here

static MyObject[] myO = { new MyObject { i1 = 1, s1 = "1", d1 = 1.0 },
                          new MyObject { i1 = 2, s1 = "2", d1 = 2.0 },
                        };

Assuming you have public fields/properties on your class:

class MyObject
{
    public int i1 { get; set; }
    public string s1 { get; set; }
    public double d1 { get; set; }
}   // note: no semicolon needed here

static MyObject[] myO = { new MyObject { i1 = 1, s1 = "1", d1 = 1.0 },
                          new MyObject { i1 = 2, s1 = "2", d1 = 2.0 },
                        };
靖瑶 2024-11-16 02:01:46

您需要用对象实例填充数组。

创建一个带有参数的构造函数,然后编写

new MyObject[] { new MyObject(a, b, c), new MyObject(d, e, f) }

You need to fill the array with object instances.

Create a constructor that takes parameters, then write

new MyObject[] { new MyObject(a, b, c), new MyObject(d, e, f) }
澉约 2024-11-16 02:01:46

您必须使用新的对象实例初始化数组。

class MyObject 
{
    int i1;
    string s1;
    double d1;

    public MyObject(int i, string s, double d)
    {
        i1 = i;
        s1 = s;
        d1 = d;
    }
};

static MyObject[] myO = new MyObject[] { 
    new MyObject(1, "1", 1.0), 
    new MyObject(2, "2", 2.0)
};

不幸的是,无法像内置类型或字典的数组一样指定自定义初始值设定项。为了(未来)参考我的意思:

int[] arr = { 1, 2, 3, 4 };
var list = new List<string> { "abc", "def" };
var dict = new Dictionary<string, int> { { "abc", 1 }, { "def", 2 } };

You'll have to initialize the array with new object instances.

class MyObject 
{
    int i1;
    string s1;
    double d1;

    public MyObject(int i, string s, double d)
    {
        i1 = i;
        s1 = s;
        d1 = d;
    }
};

static MyObject[] myO = new MyObject[] { 
    new MyObject(1, "1", 1.0), 
    new MyObject(2, "2", 2.0)
};

Unfortunately there is no way to specify custom initializers like they are for arrays of built-in types or dictionaries. For (future) reference of what I mean:

int[] arr = { 1, 2, 3, 4 };
var list = new List<string> { "abc", "def" };
var dict = new Dictionary<string, int> { { "abc", 1 }, { "def", 2 } };
述情 2024-11-16 02:01:46

您需要实例化数组中的对象:

static MyObject[] myO = new MyObject
{
   new MyObject { i1 = 1, s1 = "1", d1 = 1.0 },
   new MyObject { i1 = 2, s1 = "2", d1 = 2.0 },
};

You'll need to instantiate the objects in the array:

static MyObject[] myO = new MyObject
{
   new MyObject { i1 = 1, s1 = "1", d1 = 1.0 },
   new MyObject { i1 = 2, s1 = "2", d1 = 2.0 },
};
听风念你 2024-11-16 02:01:46

这正是您想要的,但您可以通过以下代码实现您的目标:

class MyObject
{
    public int i1;
    public string s1;
    public double d1;
};

static MyObject[] myO = new[]  { new MyObject { i1=1, s1="1", d1=1.0 }, new MyObject { i1=2, s1="2", d1=2.0 } };

It is exactly what you want but you can achieve your goal with following code:

class MyObject
{
    public int i1;
    public string s1;
    public double d1;
};

static MyObject[] myO = new[]  { new MyObject { i1=1, s1="1", d1=1.0 }, new MyObject { i1=2, s1="2", d1=2.0 } };
海拔太高太耀眼 2024-11-16 02:01:46

我在这里看到一些问题。首先,您的所有变量都是私有的。其次,您没有调用构造函数。

    class MyObject
    {
        public MyObject(int i1, string s1, double d1)
        {
            this.i1 = i1;
            this.s1 = s1;
            this.d1 = d1;
        }

        int i1;
        string s1;
        double d1;
    };

    static MyObject[] objects = new MyObject[] { new MyObject(1, "2", 3), new MyObject(1, "2", 3) };

I see a few things wrong here. Firstly all your variables are private. Secondly you are not calling a constructor.

    class MyObject
    {
        public MyObject(int i1, string s1, double d1)
        {
            this.i1 = i1;
            this.s1 = s1;
            this.d1 = d1;
        }

        int i1;
        string s1;
        double d1;
    };

    static MyObject[] objects = new MyObject[] { new MyObject(1, "2", 3), new MyObject(1, "2", 3) };
无远思近则忧 2024-11-16 02:01:46

尝试

class Foo
{
  public static Widget[] Widgets { get ; private set ; }
  static Foo()
  {
    Widgets = new Widget[]{ new Widget(1) , new Widget(2) , ... } ;
  }
}

Try

class Foo
{
  public static Widget[] Widgets { get ; private set ; }
  static Foo()
  {
    Widgets = new Widget[]{ new Widget(1) , new Widget(2) , ... } ;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文