用c#创建一个书籍类,其属性有编号bNo,名称bName,价格bPrice;再声明3个书籍对象并赋值,然后用泛型List集合实现对书籍对象的存储,并输出这些书籍的信息
时间: 2024-02-20 07:00:03 浏览: 115
好的,以下是c#代码实现:
```c#
using System;
using System.Collections.Generic;
public class Book
{
public int bNo { get; set; }
public string bName { get; set; }
public double bPrice { get; set; }
}
class Program
{
static void Main(string[] args)
{
// 声明3个书籍对象并赋值
Book book1 = new Book { bNo = 1, bName = "C#入门经典", bPrice = 99.9 };
Book book2 = new Book { bNo = 2, bName = "Java从入门到精通", bPrice = 89.5 };
Book book3 = new Book { bNo = 3, bName = "Python编程基础", bPrice = 79.8 };
// 使用泛型List集合存储书籍对象
List<Book> bookList = new List<Book>();
bookList.Add(book1);
bookList.Add(book2);
bookList.Add(book3);
// 输出书籍信息
foreach (Book book in bookList)
{
Console.WriteLine("编号:{0},名称:{1},价格:{2}", book.bNo, book.bName, book.bPrice);
}
}
}
```
输出结果为:
```
编号:1,名称:C#入门经典,价格:99.9
编号:2,名称:Java从入门到精通,价格:89.5
编号:3,名称:Python编程基础,价格:79.8
```
阅读全文