C#

[C#] λ‹€μ–‘ν•œ 데이터 ν˜•μ‹μ„ μ²˜λ¦¬ν•˜λŠ” μΌλ°˜ν™” ν”„λ‘œκ·Έλž˜λ°

hyonie 2024. 9. 1. 22:08
이 κΈ€μ—μ„œλŠ” μΌλ°˜ν™” ν”„λ‘œκ·Έλž˜λ°μ˜ κ°œλ…μ— λŒ€ν•΄μ„œ μ΄ν•΄ν•˜κ³ , μΌλ°˜ν™” λ©”μ„œλ“œμ™€ 클래슀의 μ‚¬μš© 방법을 μ •λ¦¬ν•œλ‹€. λ˜ν•œ, 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["λ‘˜"]);

        }
    }
}