๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ/C#

[C#] ์ผ๋ฐ˜ํ™” ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ์ปฌ๋ ‰์…˜

by HanaV 2023. 8. 22.
728x90

List<T>

ArrayList๋ž‘ ๋™์ผํ•˜์ง€๋งŒ <>์•ˆ์— ๋“ค์–ด๊ฐ„ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋งŒ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

using System;
using System.Collections;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        // ArrayList
        ArrayList arrayList = new ArrayList();
        arrayList.Add(1);
        arrayList.Add("Hello");
        arrayList.Add(3.14);

        Console.WriteLine("ArrayList:");
        foreach (var item in arrayList)
        {
            Console.WriteLine(item);
        }

        // List<int>
        List<int> intList = new List<int>();
        intList.Add(1);
        intList.Add(2);
        intList.Add(3);
        //intList.Add("test"); --> Error

        Console.WriteLine("List of integers:");
        foreach (int num in intList)
        {
            Console.WriteLine(num);
        }
    }
}

 

Queue<T>

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Queue์™€ ๋™์ผํ•˜์ง€๋งŒ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค.

using System;
using System.Collections;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        // Using Queue (non-generic)
        Queue queue = new Queue();
        queue.Enqueue("First");
        queue.Enqueue(2);
        queue.Enqueue(true);

        Console.WriteLine("Queue (non-generic):");
        while (queue.Count > 0)
        {
            Console.WriteLine(queue.Dequeue());
        }

        // Using Queue<T> (generic)
        Queue<int> intQueue = new Queue<int>();
        intQueue.Enqueue(1);
        intQueue.Enqueue(2);
        intQueue.Enqueue(3);
        //intQueue.Enqueue("test"); --> Error

        Console.WriteLine("\nQueue<int> (generic):");
        while (intQueue.Count > 0)
        {
            Console.WriteLine(intQueue.Dequeue());
        }
    }
}

 

Stack<T>

using System;
using System.Collections;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        // Using Stack (non-generic)
        Stack stack = new Stack();
        stack.Push("First");
        stack.Push(2);
        stack.Push(true);

        Console.WriteLine("Stack (non-generic):");
        while (stack.Count > 0)
        {
            Console.WriteLine(stack.Pop());
        }

        // Using Stack<T> (generic)
        Stack<int> intStack = new Stack<int>();
        intStack.Push(1);
        intStack.Push(2);
        intStack.Push(3);
        //intStack.Push("test"); --> Error

        Console.WriteLine("\nStack<int> (generic):");
        while (intStack.Count > 0)
        {
            Console.WriteLine(intStack.Pop());
        }
    }
}

 

Dictionary<TKey, TValue>

HashTable๊ณผ Dictionary ๋ชจ๋‘ ํ‚ค์™€ ๊ฐ’์„ ๋งคํ•‘ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ๋‹ค.
ํ•˜์ง€๋งŒ Hashtable์€ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋„๋ก object ํƒ€์ž…์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ’์„ ๊บผ๋‚ผ ๋•Œ๋Š” ํ˜• ๋ณ€ํ™˜์ด ํ•„์š”ํ•˜๋‹ค. ๊ทธ์— ๋น„ํ•ด Dictionary<TKey, TValue>๋Š” ์ปดํŒŒ์ผ ์‹œ์ ์— ํƒ€์ž… ์•ˆ์ •์„ฑ์„ ๋ณด์žฅํ•˜๋ฏ€๋กœ ํ˜• ๋ณ€ํ™˜ ์—†์ด ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

using System;
using System.Collections;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        // Using Hashtable (non-generic)
        Hashtable hashtable = new Hashtable();
        hashtable.Add("Key1", "Value1");
        hashtable.Add(2, "Value2");
        hashtable.Add(true, "Value3");

        Console.WriteLine("Hashtable (non-generic):");
        foreach (DictionaryEntry entry in hashtable)
        {
            Console.WriteLine(entry.Key + ": " + entry.Value);
        }

        // Using Dictionary<TKey, TValue> (generic)
        Dictionary<string, int> intDictionary = new Dictionary<string, int>();
        intDictionary.Add("One", 1);
        intDictionary.Add("Two", 2);
        intDictionary.Add("Three", 3);

        Console.WriteLine("\nDictionary<string, int> (generic):");
        foreach (var kvp in intDictionary)
        {
            Console.WriteLine(kvp.Key + ": " + kvp.Value);
        }
    }
}



728x90

"); wcs_do();