์ผ๋ฐํ ์ปฌ๋ ์ ์ object ํ์ ๊ธฐ๋ฐ์ ์ปฌ๋ ์ ์ด ๊ฐ๊ณ ์๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.
์ผ๋ฐํ ์ปฌ๋ ์ ์ ์ผ๋ฐํ์ ๊ธฐ๋ฐํด์ ๋ง๋ค์ด์ ธ ์๊ธฐ ๋๋ฌธ์ ์ปดํ์ผ ํ ๋ ์ปฌ๋ ์ ์์ ์ฌ์ฉํ ํ์์ด ๊ฒฐ์ ๋๊ณ ,
์ธ๋ฐ์๋ ํ์ ๋ณํ์ ์ผ์ผํค์ง ์๋๋ค. ๋ํ ์๋ชป๋ ํ์์ ๊ฐ์ฒด๋ฅผ ๋ด๊ฒ ๋ ์ํ๋ ํผํ ์ ์๋ค.
(object ํ์ ์ปฌ๋ ์ ์ ๋จ์ : ์ด๋ ํ ๊ฐ์ฒด๋ฅผ ๋ค ๋ด์ ์ ์๋ค๋ ์ฅ์ ๋๋ฌธ์ ํ์์ ์ผ๋ก ์ฑ๋ฅ๋ฌธ์ ๊ฐ ์์. ์ปฌ๋ ์ ์ ์์์ ์ ๊ทผํ ๋๋ง๋ค ํ์ ๋ณํ์ด ์ผ์ด๋๊ธฐ ๋๋ฌธ)
object ์ปฌ๋ ์ | ์ผ๋ฐํ ์ปฌ๋ ์ |
ArrayList | List<T> |
Queue | Queue<T> |
Stack | Stack<T> |
Hashtable | Dictionary<TKey,TValue> |
List<T>, Queue<T>, Stack<T>, Dictionary<TKey, TValue>๋ ๊ฐ๊ฐ
ArrayLisst, Queue, Stack, Hashtable์ ์ผ๋ฐํ ๋ฒ์ ์ด๋ค.
์ฐ๊ธฐ ์ํด์
ํด๋น ์ฝ๋๊ฐ ํ์ํ๋ค.
using System.Collections.Generic;
<List<T>์์ >
using System;
using System.Collections.Generic;
namespace UsingGenericList
{
class MainApp
{
static void Main(string[] args)
{
List<int> list = new List<int>();
for (int i = 0; i < 5; i++) list.Add(i);
foreach (int e in list) Console.Write($"{e}");
Console.WriteLine();
list.RemoveAt(2);
foreach (int e in list) Console.Write($"{e}");
Console.WriteLine();
list.Insert(2, 2);
foreach (int e in list) Console.Write($"{e} ");
Console.WriteLine();
}
}
}
<์ถ๋ ฅ ๊ฒฐ๊ณผ>
01234
0134
0 1 2 3 4
<Queue<T>์์ >
using System;
using System.Collections.Generic;
namespace UsingGenericQueue
{
class MainApp
{
static void Main(string[] args)
{
Queue<int> queue = new Queue<int>();
for (int i = 0; i < 5; i++) queue.Enqueue(i);
while (queue.Count>0) Console.WriteLine(queue.Dequeue());
}
}
}
<์ถ๋ ฅ ๊ฒฐ๊ณผ>
0
1
2
3
4
<Stack<T>์์ >
using System;
using System.Collections.Generic;
namespace UsingGenericStack
{
class MainApp
{
static void Main(string[] args)
{
Stack<int> stack = new Stack<int>();
for (int i = 0; i < 5; i++) stack.Push(i);
while (stack.Count>0) Console.WriteLine(stack.Pop());
}
}
}
<์ถ๋ ฅ ๊ฒฐ๊ณผ>
4
3
2
1
0
<Dictionary<TKey, Tvalue> ์์ >
using System;
using System.Collections.Generic;
namespace UsingGenericDictionary
{
class MainApp
{
static void Main(string[] args)
{
Dictionary<string,string> dictionary = new Dictionary<string,string>();
dictionary["ํ๋"] = "one";
dictionary["๋"] = "two";
dictionary["์
"] = "three";
dictionary["๋ท"] = "four";
dictionary["๋ค์ฏ"] = "five";
Console.WriteLine(dictionary["ํ๋"]);
Console.WriteLine(dictionary["๋"]);
Console.WriteLine(dictionary["์
"]);
Console.WriteLine(dictionary["๋ท"]);
Console.WriteLine(dictionary["๋ค์ฏ"]);
}
}
}
<์ถ๋ ฅ ๊ฒฐ๊ณผ>
one
two
three
four
five
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
60. ์์ธ ์ฒ๋ฆฌํ๊ธฐ(1) (0) | 2022.04.09 |
---|---|
59. foreach๋ฅผ ์ฌ์ฉํ ์ ์๋ ์ผ๋ฐํ ํด๋์ค (0) | 2022.02.28 |
57. ์ผ๋ฐํ ํ๋ก๊ทธ๋๋ฐ ์์ ๋ชจ์ (0) | 2022.02.28 |
56. ์ผ๋ฐํํ ๋, ํ์ ๋งค๊ฐ๋ณ์ ์ ์ฝ์ํค๊ธฐ (0) | 2022.02.28 |
55. foreach๊ฐ ๊ฐ๋ฅํ ๊ฐ์ฒด ๋ง๋ค๊ธฐ (0) | 2022.02.27 |