See .net fiddle link https://dotnetfiddle.net/E4eCWl
I have a homework assignment to use an ArrayList of type Book so while I know List<T>
would be the correct way to handle the issue I need to use an ArrayList as that is part of the assignment
so used
var books = new ArrayList<Book> {new Book("Moby Dick", 254)};
So bare with me here, my understanding here is that the once I add the custom type Book to the Arraylist I am creating a custom class as Resharper has helped me create a new class to accommodate the adding of the book, so a new class has been added, but now I am stuck, as I have no idea on how to implement the Add method or what to use the GetEnumerator for..
Any tips or website I can go to to help me further or have I gone down the wrong path to begin with on creating the type.
Custom Book Class
class Book
{
public string Title { get; set; }
public int NumberOfPages { get; set; }
public Book(string title, int pages)
{
Title = title;
NumberOfPages = pages;
}
}
Generated Class
class ArrayList<T> : IEnumerable
{
internal void Add(Book book)
{
throw new NotImplementedException();
}
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
I suspect you're meant to be using System.Collections.ArrayList
which is non-generic - so you'd have:
var books = new ArrayList { new Book("Moby Dick", 254) };
Of course, using non-generic collections is generally a bad idea, and unless the assignment is specifically making a point about the "somewhat-legacy" collections, I would question the usefulness of it. (Basically, it sounds like this assignment may just be very old and in sore need of an update. If you can possibly give feedback about this, do so!)
See more on this question at Stackoverflow