아디봉의.net
c# 제네릭(Generic) 완벽정리해보쟈! 본문
제네릭이란? 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)
class Top<T>
{
private T data = default(T);
public void SetData(T data)
{
this.data = data;
}
public T GetData()
{
return this.data;
}
}
class TopMain
{
public static void Main()
{
string str = "Hello Generic 방식";
Top<String> top = new Top<string>();
Top<object> obj = new Top<object>();
Top<int> intt = new Top<int>();
Top<double> dou = new Top<double>();
top.SetData(str);
String str2 = top.GetData();
Console.WriteLine(str2);
}
}
}
* generic method 예제
class 제네릭{
public void AA<T>(T value) {
console.write(value);
}
public void T[] createArray<T>(int size, T initVaue)
{
T[] arr = new T[size];
for(int i =0; i<size; i++)
arr[i]= initVaule;
return arr;
}
public static void main()
{
AA<int>(2007);
AA<string>("이런된장");
AA<double>(1.005);
string[] arr = createArry<string>(3, "안녕, 제네릭");
console.writeline("arr.length : {0}", arr.length);
foreach(string o in arr)
{
prn<string>(o)
}
}
}
** 제네릭 컬렉션
class generic
{
//Dictionery
public static void testDictionary()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.add("txt","notepad.txt");
dic.add("bmp","paint.exe");
dic.add("mp3","foobar.exe");
Console.WriteLine([Dictionary]);
foreach(DicBasker<string, string> DicBk in dic)
{
Console.Write("key={0} value={1}",DicBk.Key, DicBk.Value);
}
Console.WriteLine();
}
public static void testLinkedList()
{
LinkedList<string> genLL = new LinkedList<string>();
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<string> genList = new List<string>();
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<int> genQueue = new Queue<int>();
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<int> genStack = new Stack<int>();
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<T>
{
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 <arr.length; i++)
{
yield return arr[i];
}
}
}
class Program
{
public static void main()
{
ARRAY4ALL<int> nArr = new ARRAY4ALL<int>(5);
ARRAY4ALL<double> dArr = new ARRAY4ALL<double>(5);
ARRAY4ALL<string> strArr = new ARRAY4ALL<string>(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<N, C>
{
public N name;
public C classNumber;
}
public class STUDENT<T>
{
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.Length; i++)
yield return arr[i];
}
}
public class Program
{
public static void main()
{
STUDENT<NameTag<string, int>> arr
= new STUDENT<nameTag<string, int>>(3);
Random rnd = new Random();
NameTag<string, int> 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<string, int>o in arr)
{
Console.WirteLine("{0}{1}",o.name,o.classNumber);
}
}
}
}
출처 : http://blog.naver.com/thx4alice?Redirect=Log&logNo=110023499903
'C#' 카테고리의 다른 글
[C#] getter, setter 의 사용 (0) | 2012.07.25 |
---|---|
[펌] visual 2010 도움되는기능 (0) | 2012.07.12 |
[펌] 01.overview of Attributes (0) | 2012.07.09 |
[펌] 02 Custums Attributes 정의 (0) | 2012.07.09 |
[C#] .NET remoting 서비스 정리 (0) | 2012.05.30 |