Advertisement
Guest User

Untitled

a guest
Jul 30th, 2020
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. using System;
  2. using IteratorsAndComparators;
  3. using System.Collections.Generic;
  4.  
  5. namespace IteratorsAndComparators
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var bookOne = new Book("Animal Farm", 2003, "George Orwell");
  12.             var bookTwo = new Book("The Documents in the Case", 2002, "Dorothy Sayers", "Robert Eustance");
  13.             var bookThree = new Book("The Documents in the Case", 1930);
  14.  
  15.             var libraryOne = new library();
  16.             var libraryTwo = new library(bookOne, bookTwo, bookThree);
  17.         }
  18.     }
  19.  
  20. public class Book
  21.     {
  22.         public Book(string title, int year, params string[] authors)
  23.         {
  24.             this.Title = title;
  25.             this.Year = year;
  26.             this.Authors = authors;
  27.         }
  28.  
  29.         public string Title { get; set; }
  30.  
  31.         public int Year { get; set; }
  32.  
  33.         public IReadOnlyList<string> Authors { get; set; }
  34.  
  35.     }
  36.  
  37. public class library : IEnumerable<Book>
  38.     {
  39.         private List<Book> books;
  40.  
  41.         public library(params Book[] books)
  42.         {
  43.             this.books = new List<Book>(books);
  44.         }
  45.  
  46.         public IEnumerator<Book> GetEnumerator()
  47.         {
  48.             for (int i = 0; i < books.Count; i++)
  49.             {
  50.                 yield return books[i];
  51.             }
  52.         }
  53.  
  54.         IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement