Arrays, slicing, List<T>, Dictionary<TKey,TValue>
Create and use arrays, copy a slice, and work with List and Dictionary for dynamic items and key-based lookups.
Lesson overview
Goal: Pick the right container.
- Array: fixed size, fast indexing
- List<T>: grows/shrinks
- Dictionary<TKey,TValue>: fast key lookup
- Simple array slice via copy
Array basics
Arrays are fixed-size and indexable. Use Length and zero-based indices.
using System;
public class Program
{
public static void Main(string[] args)
{
int[] nums = new int[] { 10, 20, 30 };
Console.WriteLine("Length = " + nums.Length);
Console.WriteLine("First = " + nums[0]);
Console.WriteLine("Last = " + nums[nums.Length - 1]);
// Update by index
nums[1] = 25;
Console.WriteLine("Updated middle = " + nums[1]);
}
}
All lessons in this course
- Arrays, slicing, List , Dictionary
- foreach patterns; collection initializers
- LINQ preview: Where, Select, OrderBy, Take/Skip; deferred execution