[C#] Lec 09 - 일반화 프로그래밍
[C#] Lec 09 - 일반화 프로그래밍
일반화 프로그래밍
1
2
3
4
5
6
7
8
9
10
11
12
public static T[] CopyArray<T> (T[] source) {
T[] array = new T[source.Length];
for (int i = 0; i<source.Length; i++) {
array[i] = source[i];
}
return array;
}
public static void Main (string[] args) {
int[] a = new int[5] {1, 2, 3, 4, 5};
int[] b = CopyArray<T>(a);
}
일반화 클래스
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class List<T>
{
private T[] array = new T[3];
public T GetElement(int index)
{
return array[index];
}
public T this[int index]
{
get { return array[index];}
set {
if (index>=array.Length) {
Array.Resize<T>(ref array, index+1);
}
array[index] = value;
}
}
}
- 또다른 매개변수 입력이라고 생각하면 좋을듯.
- 인덱서와의 조합이 좋음
일반화 컬렉션
- 일반화 컬렉션 : using System.Collections.Generic;
- 비일반화 클래스 Arraylist, 일반화 클래스 List
- 비일반화 클래스 Queue, 일반화 클래스 Queue
- 비일반화 클래스 HashTable, 일반화 클래스 Dictionary<TKey, TValue>
- Dictionary <(Key값의 타입), (Value의 타입)> → <string, int>
This post is licensed under CC BY 4.0 by the author.