嵌套的结构或类存储在C#的列表中

时间:2019-01-21 08:59:12

标签: c# list struct nested

是否可以使用c#在列表中存储嵌套的结构或类?

查看以下代码段。

嵌套脚本:

struct structBooks
{
    public string strBookName;
    public string strAuthor;
    public structPubished publishedDate;
}

struct structPubished
{
    public int intDayOfMonth;
    public int intMonthOfYear;
    public int intYear;
}

另存为列表:

  static void AddBookToList()
    {
        structBooks testStruct = new structBooks();
        testStruct.strBookName = newBookName;
        testStruct.strAuthor = newAuther;
        testStruct.publishedDate.intYear = intNewYear;
        testStruct.publishedDate.intMonthOfYear = intNewMonthOfYear;
        testStruct.publishedDate.intDayOfMonth = intNewDayOfMonth;

        static List<structBooks> listBooks = new List<structBooks>();
        listBooks.Add(new structBooks()
        {
            strBookName = newBookName,
            strAuthor = newAuther,
            publishedDate.intYear = intNewYear,
            publishedDate.intMonthOfYear = intNewMonthOfYear,
            publishedDate.intDayOfMonth = intNewDayOfMonth
        });
    }

按预期创建所有testStruct的作品。

在将结构存储为列表时,strBookName和strAuthor都可以工作。但是,当涉及嵌套的PublishedDate时,Visual Studio会告诉我“无效的初始化器成员声明器”。

列表的自身是在Main方法中定义的,我刚刚添加了列表,以便您可以看到其定义方式。

我想念什么?

5 个答案:

答案 0 :(得分:3)

使用new初始化发布日期struct,就像处理structBooks一样。

  List<structBooks> listBooks = new List<structBooks>();
  listBooks.Add(new structBooks()
  {
    strBookName = "bookName",
    strAuthor = "author",
    publishedDate = new structPubished
      {
        intDayOfMonth = 1,
        intMonthOfYear = 1,
        intYear = 1000
      }
  });

答案 1 :(得分:1)

您需要使用 new 关键字

初始化结构
List<structBooks> listBooks = new List<structBooks>();
listBooks.Add(new structBooks()
{
    strBookName = "bookName",
    strAuthor = "author",
    publishedDate = new structPubished
      {
        intDayOfMonth = intNewDayOfMonth,
        intMonthOfYear = intNewMonthOfYear,
        intYear = intNewYear
      }
});

希望您还意识到,您实际上并不需要首先创建structPublish,而可以使用内置DateTime

这会将您的structBooks更改为

struct structBooks
{
    public string strBookName;
    public string strAuthor;
    public DateTime publishedDate;
}

,您可以添加为

List<structBooks> listBooks = new List<structBooks>();
  listBooks.Add(new structBooks()
  {
    strBookName = "bookName",
    strAuthor = "author",
    publishedDate = new DateTime(intNewYear,intNewMonthOfYear,intNewDayOfMonth)
  });

内置DateTime结构提供了许多其他功能,这些功能对于您的应用程序很有用。

答案 2 :(得分:0)

更改此:

testStruct.publishedDate.intYear = intNewYear;
testStruct.publishedDate.intMonthOfYear = intNewMonthOfYear;
testStruct.publishedDate.intDayOfMonth = intNewDayOfMonth;

对此:

testStruct.publishedDate = new structPublished {
  intYear = intNewYear,
  intMonthOfYear = inNewMonthOfYear,
  intDayOfMonth = intNewDayOfMonth
};

您无法通过设置其字段或属性来初始化某些东西-C#仍然不知道您要初始化的东西的类型。相反,您需要使用专门用于初始化对象的new关键字。

答案 3 :(得分:0)

这是正确的实现:

    static void AddBookToList()
    {
        structBooks testStruct = new structBooks();
        testStruct.strBookName = newBookName;
        testStruct.strAuthor = newAuther;
        testStruct.publishedDate.intYear = intNewYear;
        testStruct.publishedDate.intMonthOfYear = intNewMonthOfYear;
        testStruct.publishedDate.intDayOfMonth = intNewDayOfMonth;

        List<structBooks> listBooks = new List<structBooks>();
        listBooks.Add(new structBooks()
        {
            strBookName = newBookName,
            strAuthor = newAuther,
            publishedDate = new structPubished()
            {
                intYear = intNewYear,
                intMonthOfYear = intNewMonthOfYear,
                intDayOfMonth = intNewDayOfMonth
            }
        });
    }

创建testStruct与插入列表中的代码之间的区别是初始化方式。

这样做的时候

structBooks testStruct = new structBooks();

它使用默认构造函数初始化内部的每个对象,这就是为什么不必键入

testStruct.publishedDate = new structPubished();

不同地,当通过提供Object的值声明初始化时,必须指定所有内容。

答案 4 :(得分:0)

您需要使用nested object initializer syntax。注意,创建嵌套结构不需要new关键字。

listBooks.Add
(
    new structBooks
    {
        strBookName = newBookName,
        strAuthor = newAuther,
        publishedDate = 
        {
            intYear = 2018, 
            intMonthOfYear = 1, 
            intDayOfMonth = 2 
        }
    }
);