【问题标题】:How to initialize List, that is in struct如何初始化List,即在struct中
【发布时间】:2015-04-08 05:29:53
【问题描述】:

这里我声明了 main 之外的结构

    struct webDataStructure
    {
        public string title;
        public string mainLink;
        public string numberOfPages;
        public List<string> secondaryLinks;
    }

在main里面初始化结构

webDataStructure[] webData = new webDataStructure[400];

然后我在 for 循环中的 main 中初始化它

webData[i].secondaryLinks = new List<string>(35);

问题是 Count of List 还是 0,应该是 35

【问题讨论】:

  • List&lt;T&gt;(int) constructor 只允许您指定初始容量,而不是要创建的元素数量。您希望哪些字符串出现在列表中?你确定你需要一个结构而不是一个类吗?
  • 我也可以使用类,我会将链接存储在字符串中

标签: c# struct initialization


【解决方案1】:

如果您有固定大小的集合,为什么要使用 List 而不是 Array

struct webDataStructure
{
    public string title;
    public string mainLink;
    public string numberOfPages;
    public string[] secondaryLinks;
}

webData[i].secondaryLinks = new string[35];

【讨论】:

    猜你喜欢
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多