1 回答

TA貢獻(xiàn)1865條經(jīng)驗 獲得超7個贊
試試這個代碼
當(dāng)您調(diào)用 addbook 方法時,列表變量被初始化。所以它總是指向最后一個對象。
因此,它只指向您添加的最后一本書。
public void addbook(string t, string a, string p, string r, string i)
{
b = new List<book>()
{
new book{title=t,author=a,publisher=p,releasedate=r,ISBN=i}
};
}
->
public void addbook(string t, string a, string p, string r, string i)
{
b.Add(new book { title = t, author = a, publisher = p, releasedate = r, ISBN = i });
}
刪除方法
public void RemoveBookFromISBN(string targetISBN)
{
var target = b.Find((x) => x.ISBN == targetISBN);
if(target != null)
b.Remove(target);
}
- 1 回答
- 0 關(guān)注
- 72 瀏覽
添加回答
舉報