C# Collection Types (Array,List,Dictionary,HashTable and More)

A look at some of the C# collection types, such as simple arrays, generic lists, dictionary types, hashset and key value pairs.

1,058 words, estimated reading time 4 minutes.
Introduction to Programming with C#

This article is part of a series of articles. Please use the links below to navigate between the articles.

  1. Learn to Program in C# - Full Introduction to Programming Course
  2. Introdution to Programming - C# Programming Fundamentals
  3. Guide to C# Data Types, Variables and Object Casting
  4. C# Operators: Arithmetic, Comparison, Logical and more
  5. Application Flow Control and Control Structures in C#
  6. Introduction to Object Oriented Programming for Beginners
  7. Introduction to C# Object-Oriented Programming Part 2
  8. C# Collection Types (Array,List,Dictionary,HashTable and More)
  9. Error and Exception Handling in C#
  10. Events, Delegates and Extension Methods
  11. Complete Guide to File Handling in C# - Reading and Writing Files
  12. Introduction to XML and XmlDocument with C#
  13. What is LINQ? The .NET Language Integrated Query
  14. Introduction to Asynchronous Programming in C#
  15. Working with Databases using Entity Framework
  16. All About Reflection in C# To Read Metadata and Find Assemblies
  17. Debugging and Testing in C#
  18. Introduction to ASP.Net MVC Web Applications and C#
  19. Windows Application Development Using .Net and Windows Forms
  20. Assemblies and the Global Assembly Cache in C#
  21. Working with Resources Files, Culture & Regions in .Net
  22. The Ultimate Guide to Regular Expressions: Everything You Need to Know
C# Collection Types (Array,List,Dictionary,HashTable and More)

Along with common data types such as strings and integers, .Net provides several advanced data types that allow the developer to utilise common data structures more easily and make life a lot easier. This article will look at lists, dictionaries, hash tables, and string concatenation.

Basic Array

Arrays are used to store collections of data in a numerical indexed list. Arrays can be one-dimensional, multi-dimensional or jagged.

An array is declared using brackets after the data type, with an element count in the brackets. Arrays in C# are of a fixed length, not dynamic. This tutorial will cover dynamic arrays (ArrayList and List) later.

C#
int[] myIntArray = new int[10] // Create an array of 10 ints

Note: Although arrays are zero-indexed, the first item in the array is 0, the second is 1 and so on; when declaring arrays, you specify the total number of elements.

Arrays can be assigned individually or in a loop:

C#
int[] myIntArray = new int[10];
myIntArray[0] = 0;
myIntArray[1] = 10;
myIntArray[2] = 20;
myIntArray[3] = 30;

// Assignment via loop
for (int i=0; i<myIntArray.Length; i++)
{
  myIntArray[i] = i * 10;
}

// Foreach loop over array
foreach (int element in myIntArray)
{
  Console.WriteLine("${element}");
}

Arrays can also be assigned to a declaration instead of specifying a length.

C#
int[] myIntArray = {0,1,2,3,4,5,6,7};

Multi-dimensional Arrays

A multi-dimensional array consists of two or more elements that form a matrix. Jagged arrays are multi-dimensional arrays of arrays and are covered later.

C#
int myMultiArray = new int[5,5];
int myOtherMultiArray = int[5,2];

Assignment and access are the same as a single-dimensional array, but you must specify both elements.

C#
for (int y=0; y<=5; y++)
{
  for (int x=0; x<=5; x++)
  {
    myMultiArray[x,y] = x * y;
  }
}

Jagged Arrays

Jagged arrays, or arrays of arrays, are multi-dimensional arrays with arrays as the elements.

C#
int[][] myJaggedArray = new int[2][];

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

Array Methods

All arrays are objects that inherit methods and properties from the base Array class. These can be used to determine the number of items in an array (Length) and the number of dimensions (Rank).

Array Lengths

Its length cannot be changed once an array is declared with several elements. You must create another array if you need more than the number of elements declared. You cannot re-declare an array in C#.

C#
int[] myIntArray = new int[10];
int[] myLargerIntArray = new int[20];

myIntArray.CopyTo(myLargerIntArray, 0);

One solution to this problem is to use a List or an ArrayList, which are much more powerful than simple arrays.

ArrayList Collection Type

An ArrayList class holds values like an array, but elements can be added or removed at will (dynamic array). They offer greater functionality over an array but have a larger overhead. ArrayLists are not type-safe, meaning that each element of the ArrayList can be of a different type.

C#
using System.Collections.ArrayList;
ArrayList myArrayList = new ArrayList();
Exception ex = new Exception("My Demo Exception");

myArrayList.Add("Hello");
myArrayList.Add("World");
myArrayList.Add(10);
myArrayList.Add(ex);

foreach (Object obj in myArrayList)
{
  Console.WriteLine(obj.ToString());
}

This will output

Hello
World
10
System.Exception: My Demo Exception

Generic List Collection Type

A List is a generic type-safe version of an ArrayList. They are defined by specifying the type that they will contain. They can contain only this type of object. The type can be any object defined in C#, including classes.

C#
using System.Collections.Generic;

List<string> myList = new List<string>();

myList.Add("Hello");
myList.Add("World");
myList.Add(10); // Compiler Error

Both ArrayList and List support searching and sorting methods.

Hashtables Collection Type

Hash tables are named key Lists, where you define a key and a value when setting and getting values. Hash tables are not type-safe, so the key and value are of type objects. You can access the data using the named key as the index; however, you must check and cast the value.

C#
using System;
using System.Text;
using System.Collections;

class Program
{
    static void Main()
    {
        Hashtable myHashTable = new Hashtable();

        myHashTable.Add("bob", 27);
        myHashTable.Add(33, "fred");

        int theAge = (int)myHashTable["bob"];
    }
}

To iterate through hash table elements, you must create an IDictionaryEnumerator interface to retrieve the key and the value.

C#
IDictionaryEnumerator myEnumerator = new myHashTable.GetEnumerator();

while (myEnumerator.MoveNext())
{
  Console.WriteLine("{0}: {1}", myEnumerator.Key, myEnumerator.Value);
}

You can also use a foreach loop like this:

C#
foreach (string key in myHashTable.Keys)
{
  Console.WriteLine(key + '=' + myHashTable[key]);
}

Dictionary Collection Type

A dictionary is a type-safe version of the Hashtable. Like a List

C#
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Dictionary<string, int> myDictionary = new Dictionary<string, int>();

        myDictionary.Add("bob", 27);
        myDictionary.Add("fred", 33);

        int theAge = myDictionary["bob"];
    }
}

StringBuilder Class

A string data type is an immutable type; meaning that once it is created it cannot be changed. Strange, you may think, I do that all the time...

C#
string name = "Stan";
name = name + " Smith";
name += " Jr";

While the compiler sees no problems with this, it's not the most efficient method. Since the string object cannot be changed, the CLR will create a new string object, assign it the value of the old one plus the extra data, and dispose of the old object. If you are doing multiple operations, such as building up a result string, this method represents a performance overhead that can be eliminated using a StringBuilder.

StringBuilder allows the string to be manipulated without objects being created and disposed of each time. They can then be converted back to a string when needed.

C#
StringBuilder myStringBuilder = new StringBuilder();

myStringBuilder.Append("bob");
myStringBuilder.Append(" smith");
myStringBuilder.Append(" jr");

string myString = myStringBuilder.ToString();

There is a huge performance hit when appending to a string, and the speed increase from using a string builder is surprising. You can look at the performance increase by running this short piece of code. It will append a full stop to the previous string 100000 times.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string myString = "";
            StringBuilder mySb = new StringBuilder();

            Console.WriteLine("String Start: {0}", System.DateTime.Now);

            for (int i = 0; i < 100000; i++)
            {
                myString += ".";
            }

            Console.WriteLine("String End: {0}", System.DateTime.Now);
            Console.WriteLine("StringBuilder Start: {0}", System.DateTime.Now);

            for (int i = 0; i < 100000; i++)
            {
                mySb.Append(".");
            }

            Console.WriteLine("StringBuilder End: {0}", System.DateTime.Now);
        }
    }
}
String Start: 11/11/2011 20:57:56
String End: 11/11/2011 20:58:12
StringBuilder Start: 11/11/2011 20:58:12
StringBuilder End: 11/11/2011 20:58:12
Press any key to continue . . .

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

This post has only 1 comment. Why not join the discussion!

New comments for this post are currently closed.