Advertisement
BeloMaximka

Belov_LW_CS4

Feb 9th, 2022
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.32 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. namespace Classwork
  4. {
  5.     class BookList
  6.     {
  7.         HashSet<string> books = new HashSet<string>();
  8.         public static BookList operator +(BookList bookList, string name)
  9.         {
  10.             BookList result = (BookList)bookList.MemberwiseClone();
  11.             result.books.Add(name);
  12.             return result;
  13.         }
  14.         public static BookList operator -(BookList bookList, string name)
  15.         {
  16.             BookList result = (BookList)bookList.MemberwiseClone();
  17.             result.books.Remove(name);
  18.             return result;
  19.         }
  20.         public static bool operator ==(BookList bookList, string name)
  21.         {
  22.             return bookList.books.Contains(name);
  23.         }
  24.         public static bool operator !=(BookList bookList, string name)
  25.         {
  26.             return !bookList.books.Contains(name);
  27.         }
  28.         public string this[int index]
  29.         {
  30.             get
  31.             {
  32.                 var enumerator = books.GetEnumerator();
  33.                 bool isValue = false;
  34.                 for (int i = 0; i <= index; i++)
  35.                     isValue = enumerator.MoveNext();
  36.                 if (isValue)
  37.                     return enumerator.Current;
  38.                 else
  39.                     return null;
  40.             }
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement