μ΄ κΈμμλ μΌλ°ν νλ‘κ·Έλλ°μ κ°λ
μ λν΄μ μ΄ν΄νκ³ , μΌλ°ν λ©μλμ ν΄λμ€μ μ¬μ© λ°©λ²μ μ 리νλ€. λν, List, Queue, Stack, Dictionary μΌλ°ν 컬λ μ
μ μ’
λ₯λ ν¨κ» μ 리νμλ€.
μΌλ°ν νλ‘κ·Έλλ°μ΄λ?
- μΌλ°ν νλ‘κ·Έλλ°μ νΉμ ν κ°λ μμ 곡ν΅λ κ°λ μ μ°Ύμλ΄λ κ²μ μλ―Έ
- μ¦, νκ°μ§ μ½λλ₯Ό λ€μν λ°μ΄ν° νμμ νμ©
- μ¬λ¬ κ°μ νμ μ μ€λ²λ‘λ©νμ§ μκ³ , νλμ μ λ€λ¦ νμ <T>λ₯Ό μ¬μ©νμ¬ λͺ¨λ νμ μ μ§μ
μΌλ°ν λ©μλλ₯Ό μμ±νκ³ μ¬μ©νλ λ°©λ²
- ꡬ체μ μΈ νμ int, string μ΄λ¦ λμ νμ λ§€κ°λ³μ<T> κ° λ€μ΄κ°λ€.
- νμ λ§€κ°λ³μ <T>λ μ»΄νμΌ λ¨κ³μμ μ€μ λ°μ΄ν° νμμΌλ‘ λ³ν
- μ¬μ©λ²μ λ©μλ μ΄λ¦ λ€μ νμ λ§€κ°λ³μ μ΄λ¦μ λΆμΈλ€.
void CopyArray(T[] source, T[] target)
{
for(int i =0; i < source.Length; i++)
target[i] = source[i];
}
μμ μΌλ°ν λ©μλ
namespace CopyingArray
{
internal class Program
{
static void CopyArray<T>(T[] source, T[] target)
{
for (int i = 0; i< source.Length; i++)
{
target[i] = source[i];
}
}
static void Main(string[] args)
{
int[] source = { 1, 2, 3, 4, 5 }; // μλ³Έ λ°μ΄ν°
int[] target = new int[source.Length]; // 볡μ¬ν΄μ λ£μ λμ λ°°μ΄
CopyArray<int>(source, target); // CopyArray() νΈμΆ
foreach (int element in target)
Console.WriteLine(element);
}
}
}
- CopyArray<int>(source, target): <int>μ μ€μ λ°μ΄ν° νμμ μ λ ₯νλ©΄ int λ°μ΄ν° νμμΌλ‘ μ»΄νμΌ
μΌλ°ν ν΄λμ€λ₯Ό μμ±νκ³ μ¬μ©νλ λ°©λ²
- λ°μ΄ν° νμμ μΌλ°νν ν΄λμ€
- νμ λ§€κ°λ³μκ° μλ κ²μ μ μΈνλ©΄ 보ν΅μ ν΄λμ€μ κ°λ€.
- Array_Generic ν΄λμ€ νμ λ§€κ°λ³μ Tλ κ°μ²΄ μμ± μ μ λ ₯λ°μ νμμΌλ‘ μΉνλμ΄ μ»΄νμΌ
class ν΄λμ€_μ΄λ¦ <νμ_λ§€κ°λ³μ>
{
//...
}
class Array_Generic<T>
{
Private T[] array;
Public T GetElement(int index) {return array[index]}
}
// ν΄λμ€ μ¬μ©
Array_Generic<int> intArr = new Array_Generic<int>();
Array_Generic<double> dblArr = new Array_Generic<double>();
.NETμμ μ 곡νλ μΌλ°ν 컬λ μ μ’ λ₯μ μ¬μ© λ°©λ²
- μΌλ°ν 컬λ μ μ μΌλ°νμ κΈ°λ°ν΄μ λ§λ€μ΄μ‘κΈ° λλ¬Έμ μ»΄νμΌν λ 컬λ μ μμ μ¬μ©ν νμμ΄ κ²°μ λκ³ , μΈλ°μλ νμ λ³νμ νμ§μλλ€.
- SyStem.Collections.Generic λ€μμ€νμ΄μ€λ λ€μν 컬λ μ
ν΄λμ€λ₯Ό λ΄κ³ μμ§λ§ λ€μ λ€κ°μ§ ν΄λμ€λ§ μ 리
- List<T> β ArrayList
- Queue<T>
- Stack<T>
- Dictionary<TKey, TValue> β Hashtable
List<T>
- List<T>λ ArrayListμ μ μ¬ν κΈ°λ₯μ μ 곡
- List<T>λ μΈμ€ν΄μ€ μμ± μ νμ λ§€κ°λ³μλ₯Ό μ§μ
- μ§μ ν νμμ λ°μ΄ν°λ§ 리μ€νΈμ μΆκ° κ°λ₯, μ΄λ₯Ό ν΅ν΄ νμ μμ μ±μ 보μ₯
- λ°λ©΄, ArrayListλ λͺ¨λ νμ μ λ°μ΄ν°λ₯Ό νμ©, λ°λΌμ, νμ μμ μ±μ΄ λΆμ‘±ν μ μμ.
namespace UsingGenericList
{
internal class Program
{
static void Main(string[] args)
{
// μ λ€λ¦ 리μ€νΈ μ μΈ
List<int> list = new List<int>();
for (int i = 0; i < 5; i++)
list.Add(i);
foreach (int element in list)
Console.Write($"{element} ");
Console.WriteLine();
list.Remove(2);
foreach (int element in list)
Console.Write($"{element} ");
Console.WriteLine();
list.Insert(2, 2);
foreach (int element in list)
Console.Write($"{element} ");
Console.WriteLine();
}
}
}
Queue<T>
- Queue<T>λ λΉμΌλ°ν ν΄λμ€μΈ Queueμ λμΌν κΈ°λ₯μ μ 곡
- Queue<T> ν΄λμ€λ FIFO(μ μ μ μΆ) λ°©μμΌλ‘ λ°μ΄ν°λ₯Ό μ²λ¦¬νλ μ λ€λ¦ ν΄λμ€
- new Queue<int>()λ μλ‘μ΄ Queue<int> μΈμ€ν΄μ€λ₯Ό μμ±νμ¬ queue λ³μμ ν λΉ
- Enqueue λ©μλλ₯Ό μ¬μ©ν΄ νμ μ μ 1λΆν° 5κΉμ§ μμλλ‘ μΆκ°
- while 루νμ Dequeue λ©μλλ₯Ό μ¬μ©ν΄ νμμ μμλ₯Ό νλμ© μ κ±°νκ³ μΆλ ₯
namespace UsingGenericQueue
{
internal class Program
{
static void Main(string[] args)
{
Queue<int> queue = new Queue<int>();
queue.Enqueue(1);
queue.Enqueue(2);
queue.Enqueue(3);
queue.Enqueue(4);
queue.Enqueue(5);
while(queue.Count > 0)
Console.WriteLine(queue.Dequeue());
}
}
}
Stack<T>
- Stack<int> νμ μ μ λ€λ¦ μ€νμ μμ±
- stackκ³Ό μ°¨μ΄μ μ νμ λ§€κ°λ³μλ₯Ό μ¬μ©νλ€λ κ²
- μ€νμ LIFO(Last-In-First-Out) μμΉμ λ°λΌ λ§μ§λ§μ μΆκ°λ μμκ° λ¨Όμ μ κ±°
namespace UsingGenericStack
{
internal class Program
{
static void Main(string[] args)
{
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
stack.Push(4);
stack.Push(5);
while (stack.Count > 0)
Console.WriteLine(stack.Pop());
}
}
}
Dictionary<TKey, TValue>
- Hashtableμ μΌλ°ν λ²μ
- Tkey, TValye νμ λ§€κ°λ³μ 2κ°
namespace UsingDictionary
{
internal class Program
{
static void Main(string[] args)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic["νλ"] = "one";
dic["λ"] = "two";
Console.WriteLine(dic["νλ"]);
Console.WriteLine(dic["λ"]);
}
}
}
'C#' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[C#] Equals() λ©μλμ == μ°μ°μ μ°¨μ΄ (0) | 2024.12.15 |
---|---|
[C#] String.IsNullOrEmpty() μ μ λ©μλλ (1) | 2024.12.15 |
[C#] μμ νκ³ ν¨μ¨μ μΈ μ€λ λ μμ± λ° μ’ λ£ λ°©λ² (3) | 2024.06.03 |
[C#] νλ‘μΈμ€μ μ€λ λμ κ°λ - Main μ€λ λ μ 보 μΆλ ₯νκΈ° (2) | 2024.06.02 |
[C#] κ·Έλν½ GDI+ κ°λ κ³Ό κ°λ¨ν λν 그리기 (0) | 2024.05.22 |