2019-10-30 Queue<T>를 연습하는 예제

Console Programming/C# Console 2019. 10. 30. 13:30
using System;
using System.Collections.Generic;

namespace Syntax01
{
    public  class Test1
    {
        public Test1()
        {
            // Creates and initializes a new Queue.
            Queue myQ = new Queue();
            
            myQ.Enqueue(new Book());
            myQ.Enqueue(new Book());

            // Displays the properties and values of the Queue.           
            PrintValues(myQ);


            var obj = myQ.Dequeue();
            this.PrintValues(myQ);

            Console.WriteLine($"Dequeue : {obj}");

            obj = myQ.Dequeue();
            this.PrintValues(myQ);

            Console.WriteLine($"Deque : {obj}");
            //Author 조앤 K. 롤링
            //Title 해리포터
            Book book = (Book)obj;
            book.Author = "조앤 K. 롤링";
            book.Title = "해리포터";

            //Enqueue obj
            myQ.Enqueue(obj);

            this.PrintValues(myQ);



        }
        public void PrintValues(Queue myCollection)
        {
            foreach (Object obj in myCollection)
            {
                if(obj is Book)
                {
                    var book = obj as Book;
                    book.Author = (book.Author == null) ? "null" : book.Author;
                    book.Title = (book.Title == null) ? "null" : book.Title;
                    Console.WriteLine("{0}, {1}", book.Author, book.Title);
                }
                else
                {
                    Console.WriteLine(obj);
                }                             
            }
            if(myCollection.Count==0)
            {
                Console.WriteLine("Nothing in Queue");

            }
            Console.WriteLine("--------------------------------");

        }
    }
}

 

: