CSharp-OOP-Advanced-Iterators-and-Comparators-Lab
CSharp-OOP-Advanced-Iterators-and-Comparators-Lab
1. Library
NOTE: You need the namespace IteratorsAndComparators.
string Title
int Year
List<string> Authors
Authors can be anonymous, one or many. A Book should have only one constructor.
Create a class Library, which should store a collection of books and implement the IEnumerable<Book> interface.
List<Book> books
A Library could be intilized without books or with any number of books and should have only one constructor.
Examples
StartUp.cs
Solution
© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 1 of 5
2. Library Iterator
NOTE: You need the namespace IteratorsAndComparators.
Extend your solution from the prevoius task. Inside the Library class create a nested class LibraryIterator, which
should implement the IEnumerator<Book> interface. Try to implement the bodies of the inherited methods by
yourself. You will need two more members:
List<Book> books
int currentIndex
Now you should be able to iterate through a Library in the Main method.
Examples
Startup.cs
Output
Animal Farm
The Documents in the Case
The Documents in the Case
© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 2 of 5
Solution
3. Comparable Book
NOTE: You need the namespace IteratorsAndComparators.
Extend your solution from the prevoius task. Implement the IComparable<Book> interface in the existing class
Book. The comparison between two books should happen in the following order:
Override the ToString() method in your Book class, so it returns a string in the format:
{title} - {year}
Change your Library class, so that it stores the books in the correct order.
Examples
Startup.cs
© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 3 of 5
Console.WriteLine(book);
}
}
Examples
Output
The Documents in the Case - 1930
The Documents in the Case - 2002
Animal Farm - 2003
Solution
4. Book Comparator
NOTE: You need the namespace IteratorsAndComparators.
Extend your solution from the prevoius task. Create a class BookComparator, which should implement the
IComparer<Book> interface and thus include the following method:
© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 4 of 5
Examples
Startup.cs
Output
Animal Farm - 2003
The Documents in the Case - 2002
The Documents in the Case - 1930
Solution
© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 5 of 5