【问题标题】:Nested struct or class stored in a list in c#存储在 C# 列表中的嵌套结构或类
【发布时间】:2019-01-21 08:59:11
【问题描述】:

是否可以使用 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 方法中定义的,我只是添加了它,以便您查看它是如何定义的。

我错过了什么?

【问题讨论】:

  • 你考虑过使用 DateTime 吗?
  • 你可以尝试使用{}创建和初始化publishedDate
  • 你不能在初始化器中使用点语法,你只能使用new。所以你不得不说publishedDate = new structPubished { intYear = ..., intMonthOfYear = ..., intDayOfMonth = ... }
  • 查看Object Initializer了解详细信息。

标签: c# list struct nested


【解决方案1】:

使用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
      }
  });

【讨论】:

    【解决方案2】:

    您需要使用 new 关键字初始化结构

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

    希望您也意识到您实际上并不需要首先创建 structPublished 并且可以使用 in-build 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)
      });
    

    inbuild DateTime 结构提供了许多其他对您的应用程序有用的功能。

    【讨论】:

    • 为此,在这种情况下这是一种更好的方法,这是我想了解的过程。答案仍然很好:)
    【解决方案3】:

    改变这个:

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

    到这里:

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

    您无法通过设置字段或属性来初始化某项内容 - C# 仍然不知道您尝试初始化的内容的类型。相反,您需要使用为初始化对象而设计的 new 关键字。

    【讨论】:

    【解决方案4】:

    这是正确的实现:

        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 的值来声明初始化时,您必须指定所有内容。

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        猜你喜欢
        • 2021-08-09
        • 1970-01-01
        • 2018-09-12
        • 2014-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-17
        • 2017-12-25
        相关资源
        最近更新 更多