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.
Arrays, slicing, List<T>, Dictionary<TKey,TValue> is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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]);
}
}
Array slice via copy
No range operator in C# 6. Make a small helper with Array.Copy to copy a slice.
using System;
public class Program
{
// Copy a slice [start, start+count) into a new array
public static int[] Slice(int[] src, int start, int count)
{
int[] dst = new int[count];
Array.Copy(src, start, dst, 0, count);
return dst;
}
public static void Main(string[] args)
{
int[] data = new int[] { 5, 6, 7, 8, 9 };
int[] middle = Slice(data, 1, 3); // {6,7,8}
Console.WriteLine("Slice len=" + middle.Length + " first=" + middle[0] + " last=" + middle[middle.Length - 1]);
}
}
List<T> basics
List<T> resizes automatically. Use Add/Insert/Remove and Count.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
List<string> items = new List<string>();
items.Add("apple");
items.Add("banana");
items.Insert(1, "kiwi"); // apple, kiwi, banana
Console.WriteLine("Count = " + items.Count);
Console.WriteLine("Second = " + items[1]);
items.Remove("apple");
Console.WriteLine("After remove, first = " + items[0]);
}
}
Dictionary basics
Dictionary stores key-value pairs. Use ContainsKey/TryGetValue for safe lookups.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
Dictionary<string, int> ages = new Dictionary<string, int>();
ages["Ada"] = 28; // add
ages["Alan"] = 35; // add
ages["Alan"] = 36; // update
int value;
if (ages.TryGetValue("Ada", out value))
{
Console.WriteLine("Ada age = " + value);
}
Console.WriteLine("Has Grace? " + ages.ContainsKey("Grace"));
}
}
Choosing collections
Choose:
- Array for fixed-size, numeric indexing
- List<T> when size changes
- Dictionary<K,V> when you have unique keys
- Copy slices with Array.Copy helper
Keyed lookup choice
Recap
Recap: Arrays are fixed-size; copy slices with Array.Copy. Use List<T> for dynamic lists and Dictionary<K,V> for fast key lookups.
Frequently asked questions
Is the “Arrays, slicing, List<T>, Dictionary<TKey,TValue>” lesson free?
Yes — the full text of “Arrays, slicing, List<T>, Dictionary<TKey,TValue>” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “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. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Arrays, slicing, List<T>, Dictionary<TKey,TValue>” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Arrays, slicing, List , Dictionary
- foreach patterns; collection initializers
- LINQ preview: Where, Select, OrderBy, Take/Skip; deferred execution