【问题标题】:My C# List is not letting me add data to it when I have a List collection within properties of a class当我在类的属性中有一个 List 集合时,我的 C# List 不允许我向其中添加数据
【发布时间】:2017-06-28 23:48:14
【问题描述】:

我做错了什么?

  1. 我需要实例化书籍列表(其他类)吗?

    clsP.Books = new List<Book>();
    
  2. 这是我在 linqpad 中得到错误的那一行

    'UserQuery.Book' 不包含采用 3 个参数的构造函数

    var book1 = new Book("Neuromancer", "William Gibson", "Science Fiction");
    

这是完整的代码,有什么问题?

void Main()
{       
    var clsPList = new List<clsPerson>();       
    var clsP = new clsPerson();
    clsP.FirstName = "jack";
    clsP.LastName = "barker";
    clsP.Gender = "male";       

    //clsP.Books = new List<Book>();
    var book1 = new Book("Neuromancer", "William Gibson", "Science Fiction");

    clsP.Books.Add(book1);
    clsPList.Add(clsP);         
    clsPList.Dump();                        
}

public class clsPerson
{
    public string FirstName;
    public string LastName;
    public string Gender;
    public List<Book> Books = new List<Book>();
}

public class Book
{
    public string Title;
    public string Author;
    public string Genre;        
}

【问题讨论】:

  • 图书类只有零参数的默认构造函数。
  • 它没有带任何参数的构造函数,所以只需像使用 clsPerson 对象一样初始化属性。

标签: c# .net list collections


【解决方案1】:

错误是正确的。您需要为Book 提供有效的构造函数...

public class Book
{
    public Book(string title, string author, string genre)
    {
        Title = title;
        Author = author;
        Genre = genre;
    }

    public string Title;
    public string Author;
    public string Genre;
}

...或者直接分配给属性:

var book1 = new Book {
                         Title = "Neuromancer",
                         Author = "William Gibson",
                         Genre = "Science Fiction"
                     };

【讨论】:

  • 或者在类构造之后分配属性,就像你对clsPerson类所做的那样。
  • 方法必须有返回类型
  • public Book(string title, string author, string genre)
【解决方案2】:

正如错误所说,Book 没有带 3 个参数的构造函数。 您的选项是内联实例化属性,例如。

var book1 = new Book() 
{
    Title = "Neuromancer", 
    Author = "William Gibson", 
    Genre "Science Fiction"
};

或者在书上声明一个构造函数,例如。

public class Book
{
    public string Title;
    public string Author;
    public string Genre;  

    public Book (string title, string author, string genre)
    {
        Title = title;
        Author = author;
        Genre = genre;
    }      
}

【讨论】:

    【解决方案3】:

    我是否需要实例化书籍列表(其他类)

    在这种情况下,您不需要它,因为您使用空列表初始化 Books 字段:

    public List<Book> Books = new List<Book>();
    

    如果没有此分配字段,将使用默认值 null 进行初始化。

    list 是我得到错误的那一行

    您尚未在 Book 类中指定任何构造函数。所以只有默认的 parameterless 构造函数可用。要么使用对象初始化器:

    var book1 = new Book { 
         Title = "Neuromancer",
         Author = "William Gibson",
         Genre = "Science Fiction"
        };
    

    或者用三个参数创建构造函数,然后你的代码将被编译而不会出错

    public class Book
    {
        public Book(string title, string author, string genre)
        {
            Title = title;
            Author = author;
            Genre = genre;
        }
    
        public string Title;
        public string Author;
        public string Genre;        
    }
    

    另外一点 - 最好使用 properties 而不是公共字段:

    public string Title { get; set; }
    

    【讨论】:

    • 如果我从公共字段更改为像 public string Title {get; set;} 这样的属性,我是否也应该更改 Book 构造函数?
    猜你喜欢
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 2012-08-30
    • 2021-11-05
    • 1970-01-01
    • 2014-11-03
    相关资源
    最近更新 更多