As your programs grow in complexity, you’ll often need to store and manage multiple values — not just one. Instead of creating dozens of variables, C# gives you powerful tools like lists and collections that make it easy to group and manipulate data.
This is where we’ll cover the most common and useful collections in C#, focusing on arrays, List, and briefly introducing other collections. You’ll learn how to store, access, loop through, and modify items in a way that’s efficient and easy to read.
🧺 What Is a Collection?
A collection is a container that holds multiple items (like numbers, names, or objects). Collections help you:
- Store and group related data
- Loop through multiple values
- Add, remove, or update items dynamically
🔢 Arrays (Fixed-Size Collection)
An array holds a fixed number of items of the same type.
🔸 Declaring an Array
int[] numbers = new int[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
🔹 Or declare and assign in one step:
string[] fruits = { "Apple", "Banana", "Cherry" };
🔁 Looping Through an Array:
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
🚨 Arrays are fixed in size — once declared, they can’t grow or shrink.
📋 Lists (Flexible and Dynamic)
A List<T> is a generic collection that can grow and shrink. It’s more powerful and flexible than arrays.
🔸 Add the namespace:
using System.Collections.Generic;
🔸 Create and use a list:
List<string> colours = new List<string>();
colours.Add("Red");
colours.Add("Green");
colours.Add("Blue");
foreach (string colour in colours)
{
Console.WriteLine(colour);
}
🔹 Other common List operations:
colours.Remove("Green"); // Removes the item
bool hasRed = colours.Contains("Red"); // Checks for existence
int count = colours.Count; // Number of items
colours.Clear(); // Removes all items
🧪 Example: Working with Numbers in a List
List<int> scores = new List<int> { 85, 92, 78, 96 };
int total = 0;
foreach (int score in scores)
{
total += score;
}
double average = (double)total / scores.Count;
Console.WriteLine("Average Score: " + average);
📦 Array vs List – When to Use
| Feature | Array | List<T> |
|---|---|---|
| Size | Fixed | Dynamic (can grow/shrink) |
| Performance | Slightly faster | More flexible and feature-rich |
| Common Use | Known-size data, e.g. days of week | Variable-size user input, data processing |
🔁 Bonus: Other Useful Collections
As you grow in C#, explore:
Dictionary<TKey, TValue>— key-value pairsHashSet<T>— unique items onlyQueue<T>andStack<T>— FIFO and LIFO structures
But for now, mastering arrays and lists will cover 90% of what you need in early-stage development.
🧠 Practice Task
Write a program that:
- Stores 5 names in a
List<string> - Asks the user to enter a name
- Checks whether it exists in the list and prints an appropriate message
List<string> names = new List<string> { "Alice", "Bob", "Charlie", "Dana", "Eva" };
Console.Write("Enter a name: ");
string input = Console.ReadLine();
if (names.Contains(input))
{
Console.WriteLine($"{input} is in the list.");
}
else
{
Console.WriteLine($"{input} is not found.");
}
📚 Summary
| Task | Array | List |
|---|---|---|
| Declaration | int[] arr = new int[5]; | List<int> list = new(); |
| Add values | Assign by index | .Add(value) |
| Looping | foreach (var item in arr) | Same as array |
| Resize | ❌ Not possible | ✅ Dynamic |
| Common use case | Fixed set of values | Dynamic input, growing data |
📬 Need help choosing the right collection for your project, or want guidance with collection-based challenges? Reach out to us via our Contact Page — we’re here to support your learning with code help, worksheets, or project advice.