c# - 创建一个 List<Book> c# 控制台应用程序

标签 c#

我想创建一个包含几本书的列表,其中包含书名、作者姓名和出版年份。示例:( AuthorLastName,AuthorFirstName,“书名”,年份。)

我知道如何创建 List<int> ,例如:

class Program
{
    static void Main()
    {
    List<int> list = new List<int>();
    list.Add(10);
    list.Add(20);
    list.Add(25);
    list.Add(99);
    }
}

但问题是,如果我想创建一个图书列表,我不能简单地创建一个 list<string>。或 list<int>因为我希望它包含字符串和整数(如上例)。

那么,谁能解释一下我如何制作图书 list ?

最佳答案

您需要创建一个 class称为 Book包含您想要的属性。然后你可以实例化一个List<Book> .

例子:

public class Book
{
   public string AuthorFirstName { get; set; }
   public string AuthorLastName { get; set; }
   public string Title { get; set; }
   public int Year { get; set; }
}

然后,使用它:

var myBookList = new List<Book>();
myBookList.Add(new Book { 
                         AuthorFirstName = "Some", 
                         AuthorLastName = "Guy", 
                         Title = "Read My Book", 
                         Year = 2013 
                        });

关于c# - 创建一个 List<Book> c# 控制台应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14963268/

相关文章:

c# - 如何在 GridView 的 RowDataBound 中使用模板字段项

c# - 如何通过dll从c#获取c数组?

c# - RestSharp Authenticator 跟随 302 重定向

c# - 是否可以将服务帐户与谷歌云打印一起使用

c# - 如何在 Windows 窗体中运行我的控制台应用程序代码?

c# - 在 Entity Framework 中锁定数据库行

c# - 为什么我在不同的机器上使用 DeflateStream 有不同的压缩字节数组大小

c# - DataTable 到 CSV 日期格式

c# - 将 WPF 元素本身绑定(bind)到 View 模型对象

c# - MongoDB C# 驱动程序 - 无需设置/替换即可更新