gpt4 book ai didi

c# - 一次用多个值填充 C# 结构数组

转载 作者:太空狗 更新时间:2023-10-30 00:57:34 25 4
gpt4 key购买 nike

我有一个结构数组,如下所示:

    public struct DATA
{
public int number;
public int channel;
public string filename;
public string description;
}
DATA[] myData = new DATA[12];

我需要在这里填写很多值,例如 myData[0] = {0, 1, "myfile", "TBD"} 等。

用数据填充这 12 个结构的最简单方法(就 LOC 而言)是什么?改用类会更容易吗?

最佳答案

执行此操作的 C 方法不起作用。

DATA[] myData = new DATA[]{{1,3,"asdasd","asdasd"}, {...

您必须设置每个数据结构。

我建议添加一个构造函数

public DATA(int number, int channel, string filename, string description)
{
this.number = number;
this.channel = channel;
this.filename = filename;
this.description = description;
}

并使用构造函数填充数组

DATA[] myData = new DATA[]{
new DATA(1, 3, "abc", "def"),
new DATA(2, 33, "abcd", "defg"),
...
};

您还可以使用通用列表并以这种方式初始化它(.NET 3.5 及更高版本):

List<DATA> list = new List<DATA>()
{
new DATA(1, 3, "abc", "def"),
new DATA(2, 33, "abcd", "defg")
};

关于c# - 一次用多个值填充 C# 结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4887043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com