void CopyArray<T> (T[]soource ,T[] target)
{
for(int i=0; i<source.Length; i++)
target[i] = source[i];
}
class Array_Generic<T>
{
private T[] array;
// .....
public T GetElement(int index)
{
return array[index];
}
}
void CopyArray<T>(T[] source, T[] target) where T : struct
{
for( int i=0; i<source.Length; i++)
target[i] = source[i];
}
struct : T는 값 형식
class : T는 참조형식
New() : T는 반드시 매개변수 없는 생성자가 있어야함
기반 클래스 이름 : 명시한 기반 클래스의 파생클래스여야함
인터페이스 이름 : T는 명시한 인터페이스를 반드시 구현해야함, 여러개의 인터페이스를 명시할 수 있음
U : T는 또다른 형식 매개변수 U로부터 상속받은 클래스
*U 예시
class BaseArray<U> where U : Base
{
public U[] Array
{
get;
set;
}
public Base Array (int size)
{
Array new U[size];
}
public void CopyArray<T> (T[] Source) where T : U
{
Source.CopyTo(Array,0);
}
}
object 기반의 컬렉션은 기본적으로 성능문제가 있다. 일반화 컬렉션을 사용하면 이렇나 성능문제를 해결할 수 있다.
List<int> = new List<int>();
Queue<int> queue = new Queue<int>();
Stack<int> stack = new Stack<int>();
Dictionary<string,string> dic = new Dictionary<string,string>();
IEnumerable에서 형식변환이 일어나서 성능저하가 일어난다. 이를 해결하기위해 IEnumerable< T >를 사용할 수 있다.
이때 구현해야하는 메서드는
IEnumerator GetEnumerator()
IEnumerator< T > GetEnumerator()