Arrays & Collections in C# - Textnotes

Arrays & Collections in C#


Arrays and collections are used to store multiple values in a single variable. Arrays have fixed size, while collections are more flexible.

1. Arrays

1.1 One-dimensional Arrays


using System;

class Program
{
static void Main()
{
int[] numbers = new int[5]; // Declaration
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;

// Loop through array
for (int i = 0; i < numbers.Length; i++)
Console.WriteLine(numbers[i]);
}
}

Alternative Initialization:


int[] nums = { 1, 2, 3, 4, 5 };

1.2 Multi-dimensional Arrays


int[,] matrix = new int[2, 3] { {1, 2, 3}, {4, 5, 6} };

for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
Console.Write(matrix[i, j] + " ");
Console.WriteLine();
}

Output:


1 2 3
4 5 6

1.3 Jagged Arrays

An array of arrays (rows can have different lengths).


int[][] jagged = new int[3][];
jagged[0] = new int[] { 1, 2 };
jagged[1] = new int[] { 3, 4, 5 };
jagged[2] = new int[] { 6 };

for (int i = 0; i < jagged.Length; i++)
{
foreach (int num in jagged[i])
Console.Write(num + " ");
Console.WriteLine();
}

Output:


1 2
3 4 5
6

2. Collections

2.1 List<T>

Dynamic array, can grow/shrink in size.


using System;
using System.Collections.Generic;

List<string> fruits = new List<string>() { "Apple", "Banana" };
fruits.Add("Cherry");
fruits.Remove("Banana");

foreach (string fruit in fruits)
Console.WriteLine(fruit);

2.2 Dictionary<TKey, TValue>

Stores key-value pairs.


Dictionary<int, string> students = new Dictionary<int, string>();
students.Add(1, "John");
students.Add(2, "Alice");

foreach (var kvp in students)
Console.WriteLine($"ID: {kvp.Key}, Name: {kvp.Value}");

2.3 HashSet<T>

Stores unique elements, no duplicates.


HashSet<int> numbers = new HashSet<int>() { 1, 2, 3, 3 };
numbers.Add(4);

foreach (int num in numbers)
Console.WriteLine(num);

2.4 Queue<T>

First-In-First-Out (FIFO) collection.


Queue<string> queue = new Queue<string>();
queue.Enqueue("A");
queue.Enqueue("B");
queue.Enqueue("C");

Console.WriteLine(queue.Dequeue()); // Removes A

2.5 Stack<T>

Last-In-First-Out (LIFO) collection.


Stack<string> stack = new Stack<string>();
stack.Push("A");
stack.Push("B");
stack.Push("C");

Console.WriteLine(stack.Pop()); // Removes C

3. Legacy Collections: ArrayList & Hashtable

3.1 ArrayList


using System.Collections;

ArrayList list = new ArrayList();
list.Add(1);
list.Add("Hello");

foreach (var item in list)
Console.WriteLine(item);

3.2 Hashtable


Hashtable ht = new Hashtable();
ht.Add(1, "One");
ht.Add(2, "Two");

foreach (DictionaryEntry entry in ht)
Console.WriteLine($"{entry.Key} => {entry.Value}");
Note: Prefer generic collections (List<T>, Dictionary<TKey,TValue>) over legacy collections.

4. LINQ with Collections

LINQ (Language Integrated Query) allows querying arrays and collections easily.


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

List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6 };

// Get even numbers
var evens = numbers.Where(n => n % 2 == 0);

foreach (int n in evens)
Console.WriteLine(n);

// Sum of numbers
int sum = numbers.Sum();
Console.WriteLine("Sum: " + sum);

Common LINQ Methods:

  1. Where() → Filters elements
  2. Select() → Projects elements
  3. OrderBy() / OrderByDescending() → Sort
  4. Sum(), Average(), Max(), Min() → Aggregation

Summary of Chapter 6:

  1. Arrays: One-dimensional, Multi-dimensional, Jagged.
  2. Collections: Generic (List<T>, Dictionary<TKey,TValue>, HashSet<T>, Queue<T>, Stack<T>).
  3. Legacy Collections: ArrayList & Hashtable (less preferred).
  4. LINQ: Powerful queries on arrays and collections.