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,136 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. Introduction to Object Oriented Programming for Beginners
  4. Introduction to C# Object-Oriented Programming Part 2
  5. Application Flow Control and Control Structures in C#
  6. Guide to C# Data Types, Variables and Object Casting
  7. C# Collection Types (Array,List,Dictionary,HashTable and More)
  8. C# Operators: Arithmetic, Comparison, Logical and more
  9. Using Entity Framework & ADO.Net Data in C# 7
  10. What is LINQ? The .NET Language Integrated Query
  11. Error and Exception Handling in C#
  12. Advanced C# Programming Topics
  13. All About Reflection in C# To Read Metadata and Find Assemblies
  14. What Are ASP.Net WebForms
  15. Introduction to ASP.Net MVC Web Applications and C#
  16. Windows Application Development Using .Net and Windows Forms
  17. Assemblies and the Global Assembly Cache in C#
  18. Working with Resources Files, Culture & Regions in .Net
  19. The Ultimate Guide to Regular Expressions: Everything You Need to Know
  20. Introduction to XML and XmlDocument with C#
  21. Complete Guide to File Handling in C# - Reading and Writing Files

As well as common data types such as strings and integers, .Net provides several advanced data types which provide a means of utilising common data structures more easily and make life a lot easier for the developer. In this article, we 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. We will cover dynamic arrays (ArrayList and List) later in this tutorial.

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

Note: Although arrays are zero-indexed, which means that 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 on 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 need to 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 that consist of 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

Once an array is declared with several elements, its length cannot be changed. If you need more than the number of elements declared you will have to create another array. 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 is a class that holds values like an array, but elements can be added or removed at will (dynamic array). They offer greater functionality over an array, but they also 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 type of 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 List 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 object. You can access the data using the named key as the index; however, you will have to 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 a 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 efferent 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 then dispose of the old object. If you are doing multiple operations, such as building up a result string, then this method represents a performance overhead that can be eliminated by 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 have a 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 . . .
Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

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 1 comment(s). Why not join the discussion!

We respect your privacy, and will not make your email public. Learn how your comment data is processed.

  1. AA

    On Saturday 21st of January 2012, Anne-Marie Armour said

    My C# Express will not accept ...

    int[] myIntArray = int[10];

    It insists that I use the "new" keyword ...

    int[] myIntArray = new int[10];

    And it insists that ...

    int myMultiArray = int[5,5];
    int myOtherMultiArray = int[5,2];

    becomes ...

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