创建列表<;书籍>;c#控制台应用程序
我想创建一个列表,其中包含两本书,包括书名、作者姓名和出版年份。示例:(AuthorLastName,AuthorFirstName,“书名”,年份。)创建列表<;书籍>;c#控制台应用程序,c#,C#,我想创建一个列表,其中包含两本书,包括书名、作者姓名和出版年份。示例:(AuthorLastName,AuthorFirstName,“书名”,年份。) public class Book { public string AuthorLastName { get; set; } public string AuthorFirstName{ get; set; }
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;
});
我知道如何创建列表
,例如:
class Program
{
static void Main()
{
List<int> list = new List<int>();
list.Add(10);
list.Add(20);
list.Add(25);
list.Add(99);
}
}
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;
});
类程序
{
静态void Main()
{
列表=新列表();
增加(10);
增加(20);
增加(25);
增加(99);
}
}
但问题是,如果我想创建一个图书列表,我不能简单地创建一个列表
或列表
,因为我希望它包含字符串和int(如上示例)
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;
});
那么,有人能解释一下我如何列出一个书单吗 您需要创建一个名为
Book
的类
,该类包含您想要的属性。然后可以实例化一个列表
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;
});
例如:
public class Book
{
public string AuthorFirstName { get; set; }
public string AuthorLastName { get; set; }
public string Title { get; set; }
public int Year { get; set; }
}
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;
});
然后,要使用它:
var myBookList = new List<Book>();
myBookList.Add(new Book {
AuthorFirstName = "Some",
AuthorLastName = "Guy",
Title = "Read My Book",
Year = 2013
});
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;
});
var myBookList=new List();
myBookList.Add(新书{
AuthorFirstName=“Some”,
AuthorLastName=“Guy”,
Title=“阅读我的书”,
年份=2013年
});
您需要定义您的类:
public class Book
{
public string Author { get; set; }
public string Title { get; set; }
public int Year { get; set; }
}
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;
});
然后,您可以列出它们:
var listOfBooks = new List<Book>();
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;
});
var listOfBooks=new List();
像这样做
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;
});
公共课堂教材
{
公共字符串AuthorLastName{get;set;}
公共字符串AuthorFirstName{get;set;}
公共字符串标题{get;set;}
公共整数年{get;set;}
}
List lstBooks=新列表();
lstBooks.Add(新书()
{
AuthorLastName=“什么”,
AuthorFirstName=“永远”,
Title=随便什么
年份=2012年;
});
定义你的类书
非常感谢,非常有用!:)当然可以很高兴我能帮忙。@user2057693您知道您可以将此问题标记为答案吗?如果你这样做了,你将获得更多的声誉,这将有助于其他人在遇到类似问题时快速确定他们需要的信息。