Unit III Collections
Unit III Collections
1. Non-Generic Collections
These collections can store objects of different data types because they store elements as objects
(boxing/unboxing is required). They are useful when working with mixed data types but are less
efficient than generic collections.
Common Non-Generic Collections
Collection Description
ArrayList A dynamic array that automatically resizes when elements are added or removed.
Hashtable A key-value pair collection where keys are unique and hashed for fast retrieval.
using System;
using System.Collections;
class Program
{
static void Main()
{
ArrayList list = new ArrayList();
list.Add(10);
list.Add("Hello");
list.Add(3.14);
2. Generic Collections
Generic collections are type-safe and do not require boxing/unboxing, making them more
efficient.
Common Generic Collections
Collection Description
List<T> A dynamically sized list (like an ArrayList but type-safe).
Collection Description
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 10, 20, 30 };
numbers.Add(40);
numbers.Remove(20);