제네릭이란? Generic
클래스를 사용할 타입을 클래스를 디자인할 때 지정하는게 아니라 클래스를 사용할 때 지정한 후 사용하는 기술을 말한다.
[일반적으로 사용하는 클래스] class GenericTest { private Object data = null; public void Setdata(Object data){ this.data = data; } public Object Getdata() { return this.data; } } class mainTest { public static void Main() { string str = "이런이런 제네릭테스트하려궁 ㅎ ~~~"; GenericTest gt = new GenericTest(); gt.Setdata(str); string str2 = (String)gt.Getdata(); Console.WriteLine(str2); } } T - 지금은 몰라 타입 T 제네릭에서 사용된 T의 정확한 표현 - 형식매개변수(Type Parametor) * generic method 예제 class 제네릭{ public void AA(T value) { console.write(value); } public void T[] createArray (int size, T initVaue) { T[] arr = new T[size]; for(int i =0; i (2007); AA ("이런된장"); AA (1.005); string[] arr = createArry (3, "안녕, 제네릭"); console.writeline("arr.length : {0}", arr.length); foreach(string o in arr) { prn (o) } } } ** 제네릭 컬렉션 class generic { //Dictionery public static void testDictionary() { Dictionary dic = new Dictionary (); dic.add("txt","notepad.txt"); dic.add("bmp","paint.exe"); dic.add("mp3","foobar.exe"); Console.WriteLine([Dictionary]); foreach(DicBasker DicBk in dic) { Console.Write("key={0} value={1}",DicBk.Key, DicBk.Value); } Console.WriteLine(); } public static void testLinkedList() { LinkedList genLL = new LinkedList (); genLL.AddLast("4등"); genLL.Addfrist("1등"); genLL.AddAfter(genLL.Find("1등"),"2등"); genLL.AddBefore(gtnLL.Find("2등"),"3등"); Console.WriteLine("[LinkedList]"); foreach(string aa in genLL) { Console.WriteLine("value={0}",aa); } Console.WriteLine(); } public static void List() { List genList = new List (); genList.add("한국"); genList.add("중국"); genList.add("중국2"); genList.Remove("중국2"); Console.WriteLine("List"); foreach(string aa in genList) { Console.write("{0}", aa); } Console.WriteLine(); } public static void testQueue() { Queue genQueue = new Queue (); genQueue.Enqueue(1); genQueue.Enqueue(2); genQueue.Enqueue(3); Console.WriteLine("[GenericQueue]"); for(int i =0; i < 3; i++) { Console.WriteLine("value = {0}",genQueue.Dequeue); } Console.WriteLine(); } public static void testStack() { Stack genStack = new Stack (); genStack.Push(1); genStack.Push(2); genStack.Push(3); Console.WirteLink("[Stack]") for(int i = 0; i < 3 ; i++) { Console.WriteLine("value = {0}",genStack.Pop); } Console.WriteLine(); } public static void main() { testDictionary(); testLinkedList(); List(); testQueue(); testStack(); } } **generic class 테스트 class ARRAY4ALL { private t[] arr; //제네릭배열선언 public ARRAY4ALL(int size) //생성자 { arr = new T[size]; //인스터스화 } public T this[int i] { get{return arr[i];} set{arr[i] = value;} } public System.Collections.IEnumerator GetEnumerator() { for(int i =0; i nArr = new ARRAY4ALL (5); ARRAY4ALL dArr = new ARRAY4ALL (5); ARRAY4ALL strArr = new ARRAY4ALL (5); for(int i =0 ; i<5; i++) { nArr[i] = i+1; dArr[i] = i+.1; strArr[i] = "" + Convert.ToChar('A'+i); } Console.WriteLine("nArr"); foreach(int aa in nArr) Console.Write(aa); Console.WriteLine("".PadLeft(20,'-')); Console.WriteLine("dArr"); foreach(int bb in dArr) Console.Write(bb); Console.WriteLine("".PadLeft(20,'-')); Console.WriteLine("strArr"); foreach(int cc in strArr) Console.Write(cc); Console.WriteLine("".PadLeft(20,'-')); } } ** generic 응용 namespace genericArray { struct NameTag { public N name; public C classNumber; } public class STUDENT { private T[] arr; public STUDENT(int size) { arr = new T[size]; } public T this[int i] { get{ return= arr[i];} set{arr[i] = value;} } public System.Collections.IEnumerator GetEnumerator() { for(int i = 0; i > arr = new STUDENT >(3); Random rnd = new Random(); NameTag tag1, tag2, tag3; tag1.name = "봉" tag1.classNumber = rnd.Next(1000,2000); tag2.name = "몽" tag2.classNumber = rnd.Next(1000,2000); tag3.name = "진" tag3.classNumber = rnd.Next(1000,2000); arr[0] = tag1; arr[1] = tag2; arr[2] = tag3; foreach(NameTag o in arr) { Console.WirteLine("{0}{1}",o.name,o.classNumber); } } } }
'프로그램 > c#' 카테고리의 다른 글
C#의 제네릭이란? (0) | 2015.07.09 |
---|
댓글을 달아 주세요