2019-10-30 Queue<T>를 연습하는 예제
Console Programming/C# Console 2019. 10. 30. 13:30using 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("--------------------------------");
}
}
}
'Console Programming > C# Console' 카테고리의 다른 글
2019-10-31 return을 제때 사용하지 않았을 때 Indexer사용해서 인벤토리 만들기 (0) | 2019.10.31 |
---|---|
백준 2748 피보나치수2 (0) | 2019.10.31 |
2019-10-30 System.InvalidOperationException: 컬렉션이 수정되었습니다. 열거 작업이 실행되지 않을 수도 있습니다. (0) | 2019.10.30 |
백준 1003 피보나치함수 (0) | 2019.10.30 |
백준 1773 폭죽쇼 (0) | 2019.10.28 |