제네릭 메소드와 클래스 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | using System; namespace GenericEx { class ClassA { public static void CopyArray(int[] src, int[] target) { for (int i = 0; i < src.Length; ++i) { target[i] = src[i]; } } public static void CopyArray(string[] src, string[] target) { for (int i = 0; i < src.Length; ++i) { target[i] = src[i]; } } // 제네릭 메소드 public static void GenericCopyArray<T>(T[] src, T[] target) { for (int i = 0; i < src.Length; ++i) { target[i] = src[i]; } } } // 제네릭 클래스 class GenericClassA<T> { public static void GenericCopyArray(T[] src, T[] target) { for (int i = 0; i < src.Length; ++i) { target[i] = src[i]; } } } class MainClass { public static void Main (string[] args) { int[] iSrc = { 1, 2, 3, 4, 5 }; int[] iTarget = new int[5]; string[] strSrc = { "ab", "cd", "ef", "gh", "ij" }; string[] strTarget = new string[5]; ClassA.CopyArray(iSrc, iTarget); foreach (int i in iTarget) Console.WriteLine(i); ClassA.CopyArray(strSrc, strTarget); foreach (string s in strTarget) Console.WriteLine(s); float[] fSrc = { 0.1f, 0.2f, 0.3f, 0.4f, 0.5f }; float[] fTarget = new float[5]; // 제네릭 메소드의 활용 ClassA.GenericCopyArray<float>(fSrc, fTarget); foreach (float f in fTarget) Console.WriteLine(f); // 제네릭 클래스의 활용 GenericClassA<float>.GenericCopyArray(fSrc, fTarget); foreach (float f in fTarget) Console.WriteLine(f); Console.ReadKey(); } } } | cs |
LinkedList<T> 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | using System; using System.Text; using System.Collections.Generic; namespace LinkedListTEx { class MainClass { private static void Display(LinkedList<string> words, string test) { Console.WriteLine(test); foreach (string word in words) { Console.Write(word + " "); } Console.WriteLine(); Console.WriteLine(); } private static void IndicateNode(LinkedListNode<string> node, string test) { Console.WriteLine(test); if (node.List == null) { Console.WriteLine("Node '{0}' is not in the list.\n", node.Value); return; } StringBuilder result = new StringBuilder("(" + node.Value + ")"); LinkedListNode<string> nodeP = node.Previous; while (nodeP != null) { result.Insert(0, nodeP.Value + " "); nodeP = nodeP.Previous; } node = node.Next; while (node != null) { result.Append(" " + node.Value); node = node.Next; } Console.WriteLine(result); Console.WriteLine(); } public static void Main (string[] args) { string[] words = { "유니티", "C#으로", "스크립팅", "한다"}; // 연결 리스트의 생성 LinkedList<string> sentence = new LinkedList<string>(words); Display(sentence, "연결 리스트의 값은:"); Console.WriteLine("sentence.Contains(\"유니티\") = {0}", sentence.Contains("유니티")); sentence.AddFirst("오늘은"); Display(sentence, "Test 1: 리스트 앞에 '오늘은' 문자열 추가"); LinkedListNode<string> mark1 = sentence.First; sentence.RemoveFirst(); sentence.AddLast(mark1); Display(sentence, "Test 2: 마지막 단어를 끝으로 이동"); sentence.RemoveLast(); sentence.AddLast("오늘만"); Display(sentence, "Test 3: 마지막 단어를 '오늘만' 으로 변경"); mark1 = sentence.Last; sentence.RemoveLast(); sentence.AddFirst(mark1); Display(sentence, "Test 4: 마지막 노드를 첫 노드로 이동"); sentence.RemoveFirst(); LinkedListNode<string> current = sentence.FindLast("C#으로"); IndicateNode(current, "Test 5: '스크립팅'이라는 단어를 찾아 가리킨다 "); sentence.AddAfter(current, "자바스크립트로"); sentence.AddAfter(current, "그리고"); IndicateNode(current, "Test 6: '스크립팅' 단어 뒤에 단어 추가"); current = sentence.Find("유니티"); IndicateNode(current, "Test 7: '유니티' 노드를 가리킨다"); sentence.AddBefore(current, "오늘과"); sentence.AddBefore(current, "내일은"); IndicateNode(current, "Test 8: '오늘과', '내일은' 단어를 '유니티' 노드 앞에 추가"); } } } | cs |
List<T> 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | using System; using System.Collections.Generic; namespace ListTEx { public class Enemy { public string Name { get; set; } public int Level { get; set; } public int HP { get; set; } public int Exp { get; set; } public void printEnemyInfo() { Console.WriteLine ("Name : {0} Level : {1} HP : {2} Exp : {3}", Name, Level, HP, Exp); } } class MainClass { public static List<Enemy> enemyList; public static void Main (string[] args) { enemyList = new List<Enemy>(); // 추가 enemyList.Add (new Enemy () { Name = "Slime", Level = 1, HP = 200, Exp = 30 }); enemyList.Add (new Enemy () { Name = "Zombie", Level = 2, HP = 100, Exp = 50 }); enemyList.Add (new Enemy () { Name = "Skeleton", Level = 3, HP = 120, Exp = 80 }); enemyList.Add (new Enemy () { Name = "Bugbear", Level = 4, HP = 300, Exp = 150 }); // 삽입 enemyList.Insert (2, new Enemy () { Name = "Bugbear", Level = 5, HP = 350, Exp = 180 }); Console.WriteLine ("**********************"); foreach (Enemy enemy in enemyList) { enemy.printEnemyInfo(); } // 삭제 enemyList.RemoveAt (2); // 검색 후 삭제 Enemy bugBear = enemyList.Find (x => x.Name.Equals ("Bugbear")); enemyList.Remove (bugBear); Console.WriteLine ("**********************"); foreach (Enemy enemy in enemyList) { enemy.printEnemyInfo(); } } } } | cs |
Dictionary<TKey, TValue> 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | using System; using System.Collections.Generic; namespace DictionaryTEx { public class FileFormatTable { private Dictionary<string, string> openWith; public FileFormatTable() { openWith = new Dictionary<string, string>(); // 추가 openWith.Add("txt", "notepad.exe"); openWith.Add("bmp", "paint.exe"); openWith.Add("dib", "paint.exe"); openWith.Add("rtf", "wordpad.exe"); openWith.Add("odf", "wordpad.exe"); // 이미 할당 된 키일 경우 try { openWith.Add("txt", "winword.exe"); } catch { Console.WriteLine("An element with Key = \"txt\" already exists."); } // 제거 openWith.Remove("odf"); } public void printTable() { // 탐색 foreach (KeyValuePair<string, string> d in openWith) Console.WriteLine("Key = {0}, Value = {1}", d.Key, d.Value); } } class MainClass { public static void Main (string[] args) { FileFormatTable fileFormatTable = new FileFormatTable (); fileFormatTable.printTable (); } } } | cs |
Queue<T> 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | using System; using System.Collections.Generic; namespace QueueTEx { public class Enemy { public string Name { get; set; } public int Level { get; set; } public int HP { get; set; } public int Exp { get; set; } public void printEnemyInfo() { Console.WriteLine ("Name : {0} Level : {1} HP : {2} Exp : {3}", Name, Level, HP, Exp); } } class MainClass { public static Queue<Enemy> enemyQueue; public static void Main (string[] args) { enemyQueue = new Queue<Enemy>(); // 추가 enemyQueue.Enqueue (new Enemy () { Name = "Slime", Level = 1, HP = 200, Exp = 30 }); enemyQueue.Enqueue (new Enemy () { Name = "Zombie", Level = 2, HP = 100, Exp = 50 }); enemyQueue.Enqueue (new Enemy () { Name = "Skeleton", Level = 3, HP = 120, Exp = 80 }); enemyQueue.Enqueue (new Enemy () { Name = "Bugbear", Level = 4, HP = 300, Exp = 150 }); enemyQueue.Enqueue (new Enemy () { Name = "Bugbear", Level = 5, HP = 350, Exp = 180 }); Console.WriteLine ("**********************"); foreach (Enemy enemy in enemyQueue) { enemy.printEnemyInfo(); } // 삭제 Console.WriteLine ("********* Out *********"); Enemy outEnemy = enemyQueue.Dequeue (); outEnemy.printEnemyInfo (); Console.WriteLine ("**********************"); foreach (Enemy enemy in enemyQueue) { enemy.printEnemyInfo(); } // 검색 후 삭제 Console.WriteLine ("******** First *********"); Enemy firstEnemy = enemyQueue.Peek(); firstEnemy.printEnemyInfo (); } } } | cs |
Stack<T> 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | using System; using System.Collections.Generic; namespace StackTEx { public class Enemy { public string Name { get; set; } public int Level { get; set; } public int HP { get; set; } public int Exp { get; set; } public void printEnemyInfo() { Console.WriteLine ("Name : {0} Level : {1} HP : {2} Exp : {3}", Name, Level, HP, Exp); } } class MainClass { public static Stack<Enemy> enemyStack; public static void Main (string[] args) { enemyStack = new Stack<Enemy>(); // 추가 enemyStack.Push (new Enemy () { Name = "Slime", Level = 1, HP = 200, Exp = 30 }); enemyStack.Push (new Enemy () { Name = "Zombie", Level = 2, HP = 100, Exp = 50 }); enemyStack.Push (new Enemy () { Name = "Skeleton", Level = 3, HP = 120, Exp = 80 }); enemyStack.Push (new Enemy () { Name = "Bugbear", Level = 4, HP = 300, Exp = 150 }); enemyStack.Push (new Enemy () { Name = "Bugbear", Level = 5, HP = 350, Exp = 180 }); Console.WriteLine ("**********************"); foreach (Enemy enemy in enemyStack) { enemy.printEnemyInfo(); } // 삭제 Console.WriteLine ("********* Out *********"); Enemy outEnemy = enemyStack.Pop (); outEnemy.printEnemyInfo (); Console.WriteLine ("**********************"); foreach (Enemy enemy in enemyStack) { enemy.printEnemyInfo(); } // 검색 후 삭제 Console.WriteLine ("******** Last *********"); Enemy lastEnemy = enemyStack.Peek(); lastEnemy.printEnemyInfo (); } } } | cs |
'Programming > C#' 카테고리의 다른 글
[C# 프로그래밍] 8장 반복기와 양보문 (0) | 2016.01.11 |
---|---|
[C# 프로그래밍] 7장 인터페이스와 추상클래스 (0) | 2016.01.11 |
[C# 프로그래밍] 5강 컬렉션 (0) | 2016.01.11 |
[C# 프로그래밍] 4장 인덱서 (0) | 2016.01.11 |
[C# 프로그래밍] 3장 프로퍼티 (0) | 2016.01.11 |