Пример реализации IEnumerable

C Sharp > Пример реализации IEnumerable
09.02.2018 15:31:56


Наиболее часто встречающиеся слова в статье:

[Console] [WriteLine] [IEnumerable] [IComparer] [IEnumerator] [GetEnumerator] [EnumAuthor] [//foreach] [GetByPrice] [GetReverseEnum]


Статья:

class Program
    {
        static void Main(string[] args)
        {
            Books books = new Books();
 
            //foreach (Book book in books.GetByPrice())       Console.WriteLine(book);
            //foreach (Book book in books.GetReverseEnum())   Console.WriteLine(book);
            foreach (Book book in books.GetByAuthor())      Console.WriteLine(book);
 
            Console.WriteLine();
            foreach (Book book in books)        Console.WriteLine(book);
        }
    }
 
    class Book
    {
        public int    Id     { get; set; }
        public double Price  { get; set; }
        public string Title  { get; set; }
        public string Author { get; set; }
 
        public override string ToString()
        {
            return "id="+Id+" price="+Price+"$  "+Author+" : "+Title;
        }
 
        #region  sort by Author
        class SortbyAuthor : IComparer<Book>
        {
            public int Compare(Book x, Book y)
            {
                return x.Author.CompareTo(y.Author);
            }
        }
        public static IComparer<Book> ByAuthor() { return new SortbyAuthor(); }
        #endregion
 
        #region  sort by Price
        class SortbyPrice : IComparer<Book>
        {
            public int Compare(Book x, Book y)
            {
                return (int)(x.Price-y.Price);
            }
        }
        public static IComparer<Book> ByPrice() { return new SortbyPrice(); }
        #endregion
 
        public static Book[] TestBooks()
        {
            Book[] data = new Book[] 
            {
                new Book() {Id=1, Price=32, Author="Seemann", Title="Dependency Injection in .NET" },
                new Book() {Id=2, Price=42, Author="Richter", Title="CLR via C#" },
                new Book() {Id=3, Price=21, Author="Munro", Title="ASP.NET MVC 5" },
                new Book() {Id=5, Price=30, Author="Esposito", Title="Architecting Applications for the Enterprise" },
                new Book() {Id=6, Price=23, Author="Watson", Title="Writing High-Performance .NET Code" },
                new Book() {Id=7, Price=37, Author="Sharp", Title="Microsoft Visual C# 2013" },
                new Book() {Id=9, Price=11, Author="Stubblebine", Title="Regular Expression" },
                new Book() {Id=10, Price=27, Author="Liberty", Title="Learning Visual Basic .Net" },
                new Book() {Id=11, Price=62, Author="Cwalina", Title="Framework Design Guidelines" },
                new Book() {Id=12, Price=55, Author="Blewett", Title="Pro Asynchronous Programming with .NET" },
                new Book() {Id=13, Price=47, Author="Nathan", Title="Windows Presentation Foundation Unleashed" },
            };
            return data;
        }
    }
 
   class Books : IEnumerable
    {
        Book[] books;
        public Books() { books = Book.TestBooks(); }
 
        public IEnumerator GetEnumerator()
        {
            return books.GetEnumerator();
        }
        public IEnumerable GetByAuthor()
        {
            return new EnumAuthor(books);
        }
        public IEnumerable GetReverseEnum()
        {
            for (int i = books.Length-1; i >= 0; i--)
                yield return books[i];
        }
        public IEnumerable GetByPrice()
        {
            Book[] data = (Book[])books.Clone();
            Array.Sort(data, Book.ByPrice());
 
            for (int i=0; i<data.Length; ++i)
                yield return data[i];
        }
    }
 
    class EnumAuthor : IEnumerable, IEnumerator
    {
        int i = -1;
        Book[] data;
 
        public EnumAuthor(Book[] books)
        {
            data = (Book[])books.Clone();
            Array.Sort(data, Book.ByAuthor());
        }
        public IEnumerator GetEnumerator()
        {
            return this;
        }
        public object Current
        {
            get
            {
                return data[i];
            }
        }
        public bool MoveNext()
        {
            i++;
            if (i < data.Length) return true;
            else return false;
        }
        public void Reset()
        {
            i = -1;
        }
    }