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.
This article is part of a series of articles. Please use the links below to navigate between the articles.
- Learn to Program in C# - Full Introduction to Programming Course
- Introdution to Programming - C# Programming Fundamentals
- Guide to C# Data Types, Variables and Object Casting
- C# Operators: Arithmetic, Comparison, Logical and more
- Application Flow Control and Control Structures in C#
- Introduction to Object Oriented Programming for Beginners
- Introduction to C# Object-Oriented Programming Part 2
- C# Collection Types (Array,List,Dictionary,HashTable and More)
- Error and Exception Handling in C#
- Events, Delegates and Extension Methods
- Complete Guide to File Handling in C# - Reading and Writing Files
- Introduction to XML and XmlDocument with C#
- What is LINQ? The .NET Language Integrated Query
- Introduction to Asynchronous Programming in C#
- Working with Databases using Entity Framework
- All About Reflection in C# To Read Metadata and Find Assemblies
- Debugging and Testing in C#
- Introduction to ASP.Net MVC Web Applications and C#
- Windows Application Development Using .Net and Windows Forms
- Assemblies and the Global Assembly Cache in C#
- Working with Resources Files, Culture & Regions in .Net
- The Ultimate Guide to Regular Expressions: Everything You Need to Know

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.
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:
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.
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.
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.
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.
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#.
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.
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
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.
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.
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:
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
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...
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.
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.
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 . . .