제너릭 General .NET2007/12/17 10:23
MSDN - Generic 소개
1. 제너릭 클래스와 메서드가 있다.
2. 제너릭 형식에서만 가능.
3. 재사용성, 형식 안정성, 효율성 달성이 가능.
4. 컬렉션과 컬렉션을 다루는 메서드에서 일반적으로 사용.
5. System.Collections.Generic 네임스페이스에 제너릭 기반 컬렉션 클래스가 존재.
using System;
using System.Collections.Generic;
using System.Text;
namespace VS2005
{
//T형식 파라미터
public class GenericClassTest<T>
{
//중첩된 Node 클래스도 T 제너릭
public class Node
{
//T as private member data type
private T data;
public T Data
{
get { return data; }
set { data = value; }
}
private Node head;
public Node next;
public Node Next
{
get { return next; }
set { next = value; }
}
//중첩 Node 클래스의 생성자
//생성자가 T를 받아서 타입을 결정
public Node(T t)
{
next = null;
Data = t;
}
}
public GenericClassTest()
{
head = null;
}
public void AddHead(T t)
{
Node n = new Node(t);
n.next = head;
head = n;
}
public IEnumerator<T> GetEnumerator()
{
Node current = head;
while(current != null)
{
yield return current.Data;
current = current.Next;
}
}
}
public class GenericTest
{
public static void Main()
{
GenericClassTest<int> list = new GenericClassTest<int>();
for (int i = 0; i < 10; i++ )
{
list.AddHead(i);
}
foreach (int i in list)
{
System.Console.Write(i + "");
}
System.Console.WriteLine("\nDone");
}
}
}
'General .NET' 카테고리의 다른 글
| 닷넷 전용 어셈블리 (0) | 2007/12/17 |
|---|---|
| 델리게이트 (0) | 2007/12/17 |
| 제너릭 (0) | 2007/12/17 |
| C# '전처리기' 지시문 (0) | 2007/12/17 |
| XML in the .NET Framework (0) | 2007/12/17 |
| XML Data (0) | 2007/12/17 |
