1. 컬렉션의 성능 문제
컬렉션들 (ArrayList, Queue, Stack, Hashtable) 은 성능의 문제가 있다.
컬렉션은 데이터의 어떤 타입도 전부 object 타입으로 저장하기 때문에
데이터에 접근할 때마다 본래 타입으로의 형식변환이 무수하게 일어나며
이는 곧 성능의 저하로 나타난다.
이러한 이유에서 컬렉션보다는 일반화 컬렉션을 사용한다.
일반화 컬렉션은 형식을 지정해서 사용하기 때문에 불필요한 형식 변환을 발생시키지 않으며
잘못된 형식의 객체를 담는 위험을 피할 수 있다.
일반화 컬렉션에는 List, Queue, Stack, Dictionary 등이 있는데
(이들은 ArrayList, Queue, Stack, Hashtable 의 일반화 버전이다.)
일반화 컬렉션은 System.Collections.Generic 네임스페이스 안에 있기 때문에
이들을 사용하기 위해서는 네임스페이스부터 사용 선언해주어야 한다.
using System.Collection.Generic;
이제 간단하게 일반화 컬렉션들을 살펴보자.
2. List<T>
List는 ArrayList 와 같은 기능을 하며 다른점이라면 형식매개변수 T 로 타입을 지정하고
사용해야 한다는 것이다.
물론 지정한 타입의 객체외의 다른 타입 객체는 담을 수 없다.
ArrayList 와 메소드도 동일하므로 다른 설명없이 예제를 작성해 본다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
using System;
using System.Collections.Generic;
namespace Cs_Lecture
{
class Program
{
static void Main(string[] args)
{
List<int> list1 = new List<int>();
list1.Add(11); list1.Add(22); list1.Add(33); // 11, 22, 33
list1.RemoveAt(1); // 11, 33
list1.Remove(11); // 33
list1.Insert(0, 44); // 44, 33
foreach (int element in list1)
Console.Write("{0} ", element);
Console.WriteLine();
List<string> list2 = new List<string>();
list2.Add("가"); list2.Add("나"); list2.Add("다");
list2.RemoveAt(0); // 나, 다
list2.Remove("나"); // 다
list2.Insert(1, "라"); // 다, 라
foreach (string element in list2)
Console.Write("{0} ", element);
Console.WriteLine();
}
}
}
|
3. Queue<T>
일반화 컬렉션 큐도 형식 지정빼고는 그냥 컬렉션 큐와 다를게 없다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
using System;
using System.Collections.Generic;
namespace Cs_Lecture
{
class Program
{
static void Main(string[] args)
{
Queue<int> que1 = new Queue<int>();
que1.Enqueue(11); que1.Enqueue(22); que1.Enqueue(33);
while (que1.Count > 0)
Console.Write("{0} ", que1.Dequeue()); // 11 22 33
Console.WriteLine();
Queue<string> que2 = new Queue<string>();
que2.Enqueue("가"); que2.Enqueue("나"); que2.Enqueue("다");
while (que2.Count > 0)
Console.Write("{0} ", que2.Dequeue()); // 가 나 다
Console.WriteLine();
}
}
}
|
4. Stack<T>
스택도 다를거 없다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
using System;
using System.Collections.Generic;
namespace Cs_Lecture
{
class Program
{
static void Main(string[] args)
{
Stack<int> stack1 = new Stack<int>();
stack1.Push(11); stack1.Push(22); stack1.Push(33);
while (stack1.Count > 0)
Console.Write("{0} ", stack1.Pop()); // 33 22 11
Console.WriteLine();
Stack<string> stack2 = new Stack<string>();
stack2.Push("가"); stack2.Push("나"); stack2.Push("다");
while (stack2.Count > 0)
Console.Write("{0} ", stack2.Pop()); // 다 나 가
Console.WriteLine();
}
}
}
|
5. Dictionary<T>
Dictionary도 이름만 바꼈지 Hashtable과 다를게 없다.
바로 예제를 보자.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
using System;
using System.Collections.Generic;
namespace Cs_Lecture
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> dt = new Dictionary<string, string>();
dt["Apple"] = "사과";
dt["Banana"] = "바나나";
dt["Orange"] = "오렌지";
Console.WriteLine("Apple : " + dt["Apple"]);
Console.WriteLine("Banana : " + dt["Banana"]);
Console.WriteLine("Orange : " + dt["Orange"]);
}
}
}
|
'IT 야놀자 > C #' 카테고리의 다른 글
무명 형식, 무명 메소드 (0) | 2019.11.05 |
---|---|
델리게이트 (Delegate) - 콜백, 체인 (0) | 2019.11.04 |
일반화 프로그래밍 (0) | 2019.10.29 |
컬렉션 (Collection) - ArrayList, Queue, Stack, Hashtable (0) | 2019.10.28 |
프로퍼티 (Property) (0) | 2019.10.25 |