CodeBlanks Logo Icon CodeBlanks

Library class

C# course · Intermediate · Lesson 66/116

Set up collection management with the Library class.

The Library class: storing collections

In this lesson you will define the Library class. It stores books and members in List<Book> and List<Member> collections. It exposes AddBook and AddMember methods for adding from the outside.

To list the books we will write a ListBooks method that uses a foreach to print each book's title with Console.WriteLine. The Library class keeps its data inside; outsiders do not touch the list directly. This is a good way to hide the internal state of the class.

Walking a collection with foreach

Inside ListBooks, a foreach loop yields each item of the collection in turn. The line foreach (var b in _books) assigns one book from the list to b on each iteration.

In the loop body we print the title with Console.WriteLine(b.Title). Unlike a for loop, foreach does not manage an index, so it is cleaner for walking collections. We will use foreach for every listing task in this project.

Exercise Add the Library class and call ListBooks

Define a Library class. Private fields: private List<Book> _books = new List<Book>(); and private List<Member> _members = new List<Member>();. Methods: public void AddBook(Book b), public void AddMember(Member m), public void ListBooks(). In ListBooks use a foreach to print each book's Title via Console.WriteLine. In Main create a Library instance, add a book and call ListBooks.

Show example solution
Write class Library { private List<Book> _books = new List<Book>(); private List<Member> _members = new List<Member>(); public void AddBook(Book b) { _books.Add(b); } public void AddMember(Member m) { _members.Add(m); } public void ListBooks() { foreach (var b in _books) Console.WriteLine(b.Title); } }. In Main add var lib = new Library(); lib.AddBook(new Book { Title="Dune", Author="Herbert" }); lib.ListBooks();
Example solution:
using System;
using System.Collections.Generic;

class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    public bool IsBorrowed { get; set; }
}

class Member
{
    public string Name { get; set; }
    public int BorrowedCount { get; set; }
}

class Library
{
    private List<Book> _books = new List<Book>();
    private List<Member> _members = new List<Member>();

    public void AddBook(Book b) { _books.Add(b); }
    public void AddMember(Member m) { _members.Add(m); }

    public void ListBooks()
    {
        foreach (var b in _books)
        {
            Console.WriteLine(b.Title);
        }
    }
}

class Program
{
    static void Main()
    {
        var book = new Book { Title = "The Hobbit", Author = "Tolkien" };
        var member = new Member { Name = "Alex" };
        Console.WriteLine(book.Title);
        Console.WriteLine(member.Name);
        var lib = new Library();
        lib.AddBook(new Book { Title = "Dune", Author = "Herbert" });
        lib.ListBooks();
    }
}

Test yourself

  1. The method to add an item to a list

    _books.____(b);
    Show answer
    _books.Add(b);
  2. Which is the Library class's List<Book> field?

    • public List<Book> Books;
    • private List<Book> _books = new List<Book>();
    • public Book _books;
    • private List<Book>;
    Show answer

    private List<Book> _books = new List<Book>(); — Correct. A private field holds an initialized List<Book>.

  3. Match each List<T> member to its job

    Add
    Appends an item
    Count
    Returns item count
    foreach
    Walks each item
    new List<T>()
    Creates an empty list

All lessons in Intermediate