创建List <book> c#c​​onsole应用程序</book>

时间:2013-02-19 17:05:55

标签: c#

我想创建一个List,其中包含几本书,书名,作者姓名和出版年份。例如:(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>,因为我希望它包含字符串和int(如上例所示) )。

那么,任何人都可以解释我如何制作书籍清单吗?

3 个答案:

答案 0 :(得分:6)

您需要创建一个名为class的{​​{1}},其中包含您想要的属性。然后,您可以实例化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; }
}

答案 1 :(得分:3)

您需要定义您的课程:

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

然后你可以列出它们:

var listOfBooks = new List<Book>();

答案 2 :(得分:2)

做这样的事情

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

            List<Book> lstBooks = new List<Book>();
            lstBooks.Add(new Book()
            {
                AuthorLastName = "What",
                AuthorFirstName = "Ever",
                Title = Whatever
                Year = 2012;
            });