正在返回在ArrayList中添加的对象

本文关键字:对象 添加 ArrayList 返回 | 更新日期: 2023-09-27 18:25:36

我正试图弄清楚如何将对象添加到数组列表中,然后返回它。我的代码如下:

        mediaTitleCollection = new ArrayList();
        public BookMedia CreateBookTitle(string title, string subtitle, string edition, string author, string genre, int weight, int units, string isbn, int pages, int chapters)
        {
            mediaTitleCollection.Add(new BookMedia(title, subtitle, edition, author, genre, weight, units, isbn, pages, chapters));
            // Return the object i have just added in mediaTitleCollection  
        }

我试了几种方法,找了半个小时,似乎都找不到合适的答案。。

提前谢谢。

正在返回在ArrayList中添加的对象

    mediaTitleCollection = new ArrayList();
    public BookMedia CreateBookTitle(string title, string subtitle, string edition, string author, string genre, int weight, int units, string isbn, int pages, int chapters)
    {  
        BookMedia result=new BookMedia(title, subtitle, edition, author, genre, weight, units, isbn, pages, chapters);
        mediaTitleCollection.Add(result);
        // Return the object i have just added in mediaTitleCollection  
        return result;
    }
return mediaTitleCollection[mediaTitleCollection.Count-1];//After adding, it returns the last object(don't need to initialize a local scope variable)
mediaTitleCollection = new ArrayList();
public BookMedia CreateBookTitle(string title, string subtitle, string edition, string author, string genre, int weight, int units, string isbn, int pages, int chapters)
{
     BookMedia book = new BookMedia(title, subtitle, edition, author, genre, weight, units, isbn, pages, chapters);
    mediaTitleCollection.Add(book );
    return book;
            // Return the object i have just added in mediaTitleCollection  
}
    mediaTitleCollection = new ArrayList();
    public BookMedia CreateBookTitle(string title, string subtitle, string edition, string author, string genre, int weight, int units, string isbn, int pages, int chapters)
    {
        BookMedia bm = new BookMedia(title, subtitle, edition, author, genre, weight, units, isbn, pages, chapters);
        mediaTitleCollection.Add(bm);
        // Return the object i have just added in mediaTitleCollection  
        return bm;
    }