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
'๐ฅ๏ธ > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C#] try catch์ throw๋ฌธ/์ (0) | 2023.08.25 |
---|---|
[์ด๊ฒ์ด C#์ด๋ค] Chapter11: ์ฐ์ต๋ฌธ์ (0) | 2023.08.22 |
[C#] ์ผ๋ฐํ ํ๋ก๊ทธ๋๋ฐ: ๋ฉ์๋, ํด๋์ค, ์ ์ฝ์กฐ๊ฑด (0) | 2023.08.22 |
[์ด๊ฒ์ด C#์ด๋ค] Chapter10: ์ฐ์ต๋ฌธ์ (0) | 2023.08.21 |
[C#] ์ธ๋ฑ์ (0) | 2023.08.21 |