Skip to content

Array and Collections

Arrays and collections are essential concepts in C# for storing and manipulating groups of related data. They allow you to work with multiple values of the same or different types efficiently.

Arrays

An array is a data structure of fixed size that store items of a single type. Arrays are indexed, which allows elements to be randomly accessed.

C#
double[] grades;
grades = new double[35];

Student[] students = new Student[45];

grades[0] = 97;

students[14] = new Student();

double[] temperatures = new double[] { 14.5, 18.6, 23.1, 34.7 };

string[] letterGrades = { "F", "D", "C", "C+", "B", "B+", "A", "A+" };

foreach(double temperature in temperatures)
{
    Console.WriteLine(temperature);
}

Warning

Inserting and deleting elements is not possible because arrays are a fixed size. However, you can create a new array with more or less elements, using the elements of the original array.

Note

The System.Array class defines methods for working with arrays.

Collections

Collections are data structures used to manage groups of related object. Although arrays can store references to objects, collections provide a more flexible way to work with groups of objects. Collections can grow and shrink dynamically.

Standard vs. Generic

If the collection contains elements of a single type, you can use one of the Generic collection types. These types are found within the System.Collections.Generic namespace. Generic collections are type safe, only allowing the declared type to be added to it. When you retrieve an element, you won't have to determine the type or convert it.

Note

Microsoft recommends you avoid using standard collections. They recommend using the generic equivalent.

List<T>

Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.

C#
List<string> names = new List<string>();

List<int> numbers = new List<int> { 1, 2, 3 };

names.Add("Kenny");
names.Add("Matt");
names.Add("Nick");

Console.WriteLine(names[0]); // Kenny

foreach(string name in names)
{
    Console.WriteLine(name);
}

// Kenny
// Matt
// Nick

names.Remove("Matt");

Console.WriteLine(names.Count); // 2

names.Clear();

Dictionary

Represents a collection of key/value pairs that are organized based on the key.

C#
Dictionary<string, int> spells = new Dictionary<string, int>();

spells.Add("Smite", 1);
spells.Add("Shadow Word: Pain", 2);
spells.Add("Mind Blast", 5);

Console.WriteLine(spells.Count); // 3

Console.WriteLine(spells.ContainsKey("Smite")); // true

Console.WriteLine("Spell level: {0}", spells["Mind Blast"]); // Spell level: 5

foreach (string key in spells.Keys)
{
    Console.WriteLine("{0} is level {1}.", key, spells[key]);
}

// Smite is level 1.
// Shadow Word: Pain is level 2.
// Mind Blast is level 5.

spells.Remove("Smite");

spells.Clear();

SortedList

Represents a collection of key/value pairs that are sorted by key based on the associated IComparer<T> implementation. The SortedList will sort elements by key. Keys are sorted based on how objects of that type are sorted. Each time an element is added to the SortedList, the elements will be reordered.

C#
SortedList<string, decimal> menuItems = new SortedList<string, decimal>();

menuItems.Add("Pizza", 15);
menuItems.Add("Hot Dog", 2);
menuItems.Add("Noodles", 5);

Console.WriteLine(menuItems.Count); // 3

Console.WriteLine(menuItems.ContainsKey("Noodles")); // true

Console.WriteLine("Hot dog price: {0:C}", menuItems["Hot Dog"]); // Hot dog price: $2.00

for(int i = 0; i < menuItems.Count; i++)
{
    Console.WriteLine("{0} is {1:C}", menuItems.Keys[i], menuItems.Values[i]);
}

// Hot Dog is $2.00
// Noodles is $5.00
// Pizza is $15.00

menuItems.Remove("Pizza");
menuItems.Clear();

Danger

An ArgumentException is thrown when you attempt to add a duplicate key.

Stack<T>

Represents a last in, first out (LIFO) collection of objects. Stacks have two key operations:

  1. Push - Inserts an element onto the top of the stack.
  2. Pop - Removes the topmost element and returns it.
C#
Stack<string> friends = new Stack<string>();

friends.Push("Kenny");
friends.Push("Matt");
friends.Push("Nick");

string friend = friends.Pop();

Console.WriteLine(friend); // Nick

Console.WriteLine(friends.Count); // 2

Console.WriteLine(friends.Peek()); // Matt

friends.Clear();

Queue<T>

Represents a first in, first out (FIFO) collection of objects. Queues have two key operations:

  1. Enqueue - Inserts an element onto the end of the queue.
  2. Dequeue - Removes the element at the beginning of the queue and returns it.
C#
Queue<string> enemies = new Queue<string>();

enemies.Enqueue("Kenny");
enemies.Enqueue("Matt");
enemies.Enqueue("Nick");

string enemy = enemies.Dequeue();

Console.WriteLine(enemy); // Kenny

Console.WriteLine(enemies.Count); // 2

Console.WriteLine(enemies.Peek()); // Matt

enemies.Clear();

Further Reading